关于控件的刷新

  • 主题发起人 主题发起人 import
  • 开始时间 开始时间
I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个组件a,继承于Component,重载它的paint(),
update() 方法。 这个组件主要是实现一个动画,在paint()中通过位置来画出一幅
画。 我在一个Applet中(继承Runnable接口) 调用这个组件,在Applet的run()中
有个循环,每隔一段时间调用a.repaint(). 结果整个图象闪烁地很厉害。请教各位大
虾怎么解决?
 
每次更改图象时调用Invalidate;方法!或者在重画之前Canvas.Lock,画完之后Canvas.UnLock。
如果构件的 ComponentStyle 属性没有包含 csOpaque 旗帜的话,调用
Invalidate方法时会导致构件的背景先被擦掉再重绘。如果你在 Paint 方法中绘制
背景,那你应该在构件的建构函式中加上:
ComponentStyle := ComponentStyle + [csOpaque];
最好就是不重画背景。
这有个俄国佬写的小例子,可以借鉴一下:
unit FFPBox;
{=======================================================}
interface
{=======================================================}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TFFPaintEvent = procedure (Sender: TObject; Canvas: TCanvas) of Object;
TFlickerFreePaintBox = class(TCustomControl)
private
{ Private declarations }
FOnFFPaint:TFFPaintEvent;
protected
{ Protected declarations }
procedure Paint; override;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property OnPaint:TFFPaintEvent read FOnFFPaint write FOnFFPaint;
property Align;
property Color;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
procedure Register;
{=======================================================}
implementation
{=======================================================}
constructor TFlickerFreePaintBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
TabStop := False;
end;
{-------------------------------------------------------}
procedure TFlickerFreePaintBox.Paint;
var bmp: TBitmap;
begin
if csDesigning in ComponentState then
with Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsSolid;
Canvas.Brush.Color := Color;
Rectangle(0, 0, Width, Height);
exit;
end;
bmp := TBitmap.Create;
try
bmp.Canvas.Brush.Color := Color;
bmp.Width := Width;
bmp.Height := Height;
bmp.Canvas.Font := Font;
if Assigned(FOnFFPaint) then begin
FOnFFPaint(Self, bmp.Canvas);
Canvas.Draw(0,0, bmp);
end;
finally
bmp.Free;
end;
end;
{-------------------------------------------------------}
{------------------------关键之处-----------------------}
procedure TFlickerFreePaintBox.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1;
end;
{=======================================================}
procedure Register;
begin
RegisterComponents('System', [TFlickerFreePaintBox]);
end;
end.
 

Similar threads

S
回复
0
查看
886
SUNSTONE的Delphi笔记
S
S
回复
0
查看
863
SUNSTONE的Delphi笔记
S
I
回复
0
查看
617
import
I
I
回复
0
查看
579
import
I
后退
顶部