Delphi: How to know when a TEdit changes size?

十年热恋 提交于 2019-12-21 20:43:39

问题


i need to update items around an edit box when it changes size.

TEdit has no OnResize event.

An edit box can resize at various times, e.g.:

  • changing width/height in code
  • form scaled for DPI scaling
  • font changed

And i'm sure others i don't know about.

i need a single event to know when an edit box has changed its size. Is there a Windows message i can subclass the edit box for and grab?


回答1:


OnResize is declared as a protected property of TControl. You could expose it using a so-called "cracker" class. It's a bit of a hack, though.

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;



回答2:


Did you try something like this:

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.

or this:

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.



回答3:


Handle the wm_Size message. Subclass a control by assigning a new value to its WindowProc property; be sure to store the old value so you can delegate other messages there.

See also: wm_WindowPosChanged



来源:https://stackoverflow.com/questions/1423411/delphi-how-to-know-when-a-tedit-changes-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!