Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when the form is being maximized?

I would like to detect when the form is going to be maximized to save certain settings (not related to the size nor position) and modify the size and position a little bit. Is there an universal way to do it ? I've tried to catch the WM_SYSCOMMAND message like in this article. It works well for maximization from menu, by maximize button, but it's not fired when I press the WIN + UP keystroke. Does anyone know an universal way how to catch the maximization event including the case with WIN + UP keystroke ?

Thanks

like image 240
Martin Reiner Avatar asked Mar 14 '12 14:03

Martin Reiner


3 Answers

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;
like image 67
RRUZ Avatar answered Sep 28 '22 03:09

RRUZ


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.

like image 43
Remy Lebeau Avatar answered Sep 28 '22 05:09

Remy Lebeau


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;
like image 35
kobik Avatar answered Sep 28 '22 04:09

kobik