当编辑框大小改变时,我需要更新其周围的项目。
TEdit
没有OnResize
事件。
编辑框可能在不同的时间调整大小,例如:
- 代码中更改宽度/高度
- 为DPI缩放而调整窗体大小
- 更改字体
我需要一个单一的事件来知道何时编辑框已经改变大小。是否有Windows消息可以子类化编辑框并抓取?
当编辑框大小改变时,我需要更新其周围的项目。
TEdit
没有OnResize
事件。
编辑框可能在不同的时间调整大小,例如:
我需要一个单一的事件来知道何时编辑框已经改变大小。是否有Windows消息可以子类化编辑框并抓取?
OnResize被声明为TControl的受保护属性。您可以使用所谓的“破解类”来公开它。虽然这有点像黑客行为,但也是可行的。
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