How to detect when the form is being maximized?

こ雲淡風輕ζ 提交于 2019-12-03 12:54:49

You can use the WM_GETMINMAXINFO message to save the state of the window and then use the WMSize message to check if the window was maximized.

in you form declare the mesage handler like so :

procedure WMSize(var Msg: TMessage); message WM_SIZE;

And handle like this :

procedure TForm57.WMSize(var Msg: TMessage);
begin
  if Msg.WParam  = SIZE_MAXIMIZED then
    ShowMessage('Maximized');    
end;

WIN+UP does not generate WM_SYSCOMMAND messages, that is why you cannot catch them. It does generate WM_GETMINMAXINFO, WM_WINDOWPOSCHANGING, WM_NCCALCSIZE, WM_MOVE, WM_SIZE, and WM_WINDOWPOSCHANGED messages, though. Like RRUZ said, use WM_GETMINMAXINFO to detect when a maximize operation is about to begin and WM_SIZE to detect when it is finished.

IMO, You cannot use WM_GETMINMAXINFO to "detect when a maximize operation is about to begin" as @Remy stated.

In-fact the only message that can is WM_SYSCOMMAND with Msg.CmdType=SC_MAXIMIZE or undocumented SC_MAXIMIZE2 = $F032 - but it's not being sent via Win+UP, or by using ShowWindow(Handle, SW_MAXIMIZE) for example.

The only way I could detect that a window is about to be maximized is via WM_WINDOWPOSCHANGING which is fired right after WM_GETMINMAXINFO:

type
  TForm1 = class(TForm)
  private
    procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
  end;

implementation

const
  SWP_STATECHANGED = $8000;

procedure TForm1.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
  inherited;
  if (Message.WindowPos^.flags and (SWP_STATECHANGED or SWP_FRAMECHANGED)) <> 0 then
  begin
    if (Message.WindowPos^.x < 0) and (Message.WindowPos^.y < 0) then
      ShowMessage('Window state is about to change to MAXIMIZED');
  end;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!