德尔福:如何知道 TEdit 何时改变大小?
题
当编辑框改变大小时,我需要更新它周围的项目。
TEdit 没有 调整大小时 事件。
编辑框可以在不同时间调整大小,例如:
- 更改代码中的宽度/高度
- 针对 DPI 缩放进行缩放的表单
- 字体改变了
我确信还有其他我不知道的事情。
我需要一个事件来知道编辑框何时更改其大小。是否有 Windows 消息我可以对编辑框进行子类化并抓取?
解决方案
OnResize 被声明为 TControl 的受保护属性。您可以使用所谓的“cracker”类来公开它。不过,这有点像黑客。
type
TControlCracker = class(TControl);
...
procedure TForm1.FormCreate(Sender: TObject);
begin
TControlCracker(Edit1).OnResize := MyEditResize;
end;
procedure TForm1.MyEditResize(Sender: TObject);
begin
Memo1.Lines.Add(IntToStr(Edit1.Width));
end;
其他提示
你有没有尝试过这样的事情:
unit _MM_Copy_Buffer_;
interface
type
TMyEdit = class(TCustomEdit)
protected
procedure Resize; override;
end;
implementation
procedure TMyEdit.Resize;
begin
inherited;
if not (csLoading in ComponentState) then
begin
// react on new size
end;
end;
end.
或这个:
unit _MM_Copy_Buffer_;
interface
type
TCustomComboEdit = class(TCustomMaskEdit)
private
procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;
implementation
procedure TCustomComboEdit.WMSize(var Message: TWMSize);
begin
inherited;
if not (csLoading in ComponentState) then
begin
// react on new size
end;
UpdateBtnBounds;
end;
end.
处理 wm_Size
信息。通过为控件分配新值来对控件进行子类化 WindowProc
财产;请务必存储旧值,以便您可以在那里委托其他消息。
也可以看看: wm_WindowPosChanged
不隶属于 StackOverflow