③ 在库单元中的实现部分编写新的destructor
destructor TSampleShape.destroy;
begin
FPen.Free;
FBrush.Free;
inherited destroy;
end;
④ 设置Owned对象的属性
处理Pen和Brush对象的最后一步是处理Pen和Brush发生改变时对Shape控制的重画问题。Pen和Brush对象都有OnChange事件,因此能够在Shape控制中声明OnChange事件指向的事件处理过程。
下面给Shape控制增加了该方法并更新了部件的constructor以使Pen和Brush事件指向新方法:
type
TSampleShape = class(TGraphicControl)
published
procdeure StyleChanged(Sender: TObject);
end;
implemintation
constructor TSampleShape.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
Width := 65;
Height := 65;
Fpen := TPen.Create;
FPen.OnChange := StyleChanged;
Fbrush := TBrush.Create;
FBrush.OnChange := StyleChanged;
end;
procedure TSampleShape.StyleChanged(Sender: TObject);
begin
Invalidate(true);
end;
当变化发生时,部件重画以响应Pen或Brush的改变。
4. 怎样画部件图形
图形控制基本要素是在屏幕上画图形的方法。抽象类TGraphicControl定义了名为Paint的虚方法,可以覆盖该方法来画所要的图形。
Shape控制的paint方法需要做:
● 使用用户选择的Pen和Brush
● 使用所选的形状
● 调整座标。这样,方形和圆可以使用相同的Width和Height
覆盖paint
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




