[英]How to respond to changes in fields of object properties in Delphi
In Delphi 7, descend a new component from TGraphicControl
, and add a TFont
property, implement the paint method to write some string using the TFont
property. 在Delphi 7中,从
TGraphicControl
一个新组件,并添加一个TFont
属性,实现paint方法使用TFont
属性写一些字符串。 Install the component. 安装组件。
At design time when you change the TFont
property using the property dialog, it will be reflected in your component instantaneously. 在设计时,使用属性对话框更改
TFont
属性时,它将立即反映在组件中。 But when you change individual properties of TFont
like Color
or Size
, your component will not be repainted until you hover over it. 但是当您更改
TFont
各个属性(如“ Color
或“ Size
,只有将鼠标悬停在其上时才会重新绘制该组件。
How do I correctly handle changes in fields of object properties? 如何正确处理对象属性字段的更改?
Assign an event handler to the TFont.OnChange
event. 将事件处理程序分配给
TFont.OnChange
事件。 In the handler, Invalidate()
your control to trigger a repaint. 在处理程序中,
Invalidate()
您的控件以触发重绘。 For example: 例如:
type
TMyControl = class(TGraphicControl)
private
FMyFont: TFont;
procedure MyFontChanged(Sender: TObject);
procedure SetMyFont(Value: TFont);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MyFont: TFont read FMyFont write SetMyFont;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
FMyFont := TFont.Create;
FMyFont.OnChange := MyFontChanged;
end;
destructor TMyControl.Destroy;
begin
FMyFont.Free;
inherited;
end;
procedure TMyControl.MyFontChanged(Sender: TObject);
begin
Invalidate;
end;
procedure TMyControl.SetMyFont(Value: TFont);
begin
FMyFont.Assign(Value);
end;
procedure TMyControl.Paint;
begin
// use MyFont as needed...
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.