Bo *_*eal 3 delphi components properties
我有一个从TGraphicControl下降的自定义Delphi组件.它的类声明如下:
TMyLabel = class(TGraphicControl)
private
...
protected
...
public
...
published
property Height;
property Width write SetWidth;
...
end;
Run Code Online (Sandbox Code Playgroud)
SetWidth的实现更进一步:
procedure TMyLabel.SetWidth(const Value: Integer);
begin
if (Value >= 0) and (Value <> Width)
then begin
inherited Width := Value;
// Do some other stuff
...
end;
MessageDlg('Test', mtInformation, [mbOK], 0);
end;
Run Code Online (Sandbox Code Playgroud)
当组件的宽度在运行时或在设计时通过在对象检查器的相应字段中输入值而以编程方式更改时,我当前调用了SetWidth.但是,当我在设计时使用鼠标调整组件大小时,对象检查器"宽度"字段会更新,但不会显示消息框,因此不会调用我的SetWidth过程.
我需要在鼠标调整组件大小时调用SetWidth,以便我可以为Paint过程设置一个标志,以便知道何时必须执行其他操作(除了重新绘制组件之外).有没有办法实现这个目标?
我现在无法检查,但是iirc我怀疑设计师使用SetBounds()调整控件的大小,而不是设置单独的left,top,width和height属性.
幸运的是,SetBounds()是虚拟的,因此您可以在自定义控件类中简单地覆盖它.
虽然Mason Wheeler的回答是你问题的答案,但我想提醒你.
覆盖属性可能会产生奇怪的结果,因为您不能拥有"虚拟"属性,而SetWidth也不是虚拟的.如果有人使用您的类的后代来设置Width属性,则不会调用您的代码.因此,我建议不要以这种方式覆盖属性.
var Control: TControl;
begin
Control := MyLabel;
Control.Width := 5000; // TMyLabel.SetWidth is not called!!
Run Code Online (Sandbox Code Playgroud)
此外:正如Deltics所解释的那样,设置Width属性并不是改变控件宽度的唯一方法.你应该重写TControl.SetBounds.
var MyLabel: TMyLabel;
begin
MyLabel.SetBounds(0, 0, 100, 100); // TMyLabel.SetWidth nor TControl.SetWidth is called!!
Run Code Online (Sandbox Code Playgroud)
但似乎你想限制你的控件的宽度.然后你最好覆盖为此目的而制作的TControl.CanResize.或者如果您只想对任何类型的调整大小做出反应,您最好覆盖TControl.Resize.
| 归档时间: |
|
| 查看次数: |
2018 次 |
| 最近记录: |