How to detect Form Resize END, maybe by using the TApplicationEvents component?

夙愿已清 提交于 2021-02-16 21:30:49

问题


In a Delphi 10.4 VCL Application, I need to detect when the FORM RESIZING ENDS. (E.g. after the user resized the Form by dragging its size-grip).

So I have placed a TApplicationEvents component on the form and created its OnMessage event-handler, trying to catch the WM_EXITSIZEMOVE message:

procedure TformMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
  if (Msg.Message = WM_EXITSIZEMOVE) then
  begin
    CodeSite.Send('TformMain.ApplicationEvents1Message: WM_EXITSIZEMOVE');
  end;
end;

But the event-handler with WM_EXITSIZEMOVE is not executed after resizing the Form.

So how can I detect the Form Resize END, maybe by using the TApplicationEvents component?


回答1:


The WM_EXITSIZEMOVE message is sent directly to the window. Thus, it is not detected by the TApplicationEvents's OnMessage handler, because that only detects messages that are posted to the main message queue.

So, you need to override the form's WndProc() instead:

type
  TForm1 = class(TForm)
  private
  protected
    procedure WndProc(var Message: TMessage); override;
  public
  end;

implementation

procedure TForm1.WndProc(var Message: TMessage);
begin
  inherited;
  case Message.Msg of
    WM_EXITSIZEMOVE:
      ShowMessage('Yes!');
  end;
end;

Or, you can use a message procedure instead:

type
  TForm1 = class(TForm)
  private
  protected
    procedure WMExitSizeMove(var Message: TMessage); message WM_EXITSIZEMOVE;
  public
  end;

implementation

procedure TForm1.WMExitSizeMove(var Message: TMessage);
begin
  inherited;
  ShowMessage('Yes!');
end;

However, beware that this message, as its name implies, is not only sent when the window has been resized, but it is also sent after the window has been moved. And in both cases, only when the operation involved a modal loop.

For instance, if you maximize the window by double-clicking its title bar, or if you move it to a different screen by pressing Shift+Win+Right, this message isn't sent at all.



来源:https://stackoverflow.com/questions/63476818/how-to-detect-form-resize-end-maybe-by-using-the-tapplicationevents-component

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