jpfollenius
jpfollenius

Reputation: 16620

Detect all situations where form is minimized

I need to detect when a form is minimized (to hide overlay form). I intercept WM_SYSCOMMAND message and it works fine if I click the form's minimize button, but this event seems not to be fired if I use [Windows] + [M]. Also, WM_ACTIVATE and WM_ACTIVATEAPP are not triggered in this case.

What event could I use and are there any other situations that I would have to detect?

Upvotes: 2

Views: 2717

Answers (3)

LU RD
LU RD

Reputation: 34929

As explained here, How to detect when the form is being maximized?, listen to the WM_SIZE messages.

Declare in your form:

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

And implementation:

procedure TForm1.WMSize(var Msg: TMessage);
begin
  Inherited;
  if Msg.WParam  = SIZE_MINIMIZED then
    ShowMessage('Minimized');
end;

Update

See also the answer by @bummi where there is a solution when Application.MainFormOnTaskbar = false.

Upvotes: 6

bummi
bummi

Reputation: 27385

Since WM_SIZE will not be called on a mainform of a project not using the setting Application.MainFormOnTaskbar := True; I'd suggest an approach, inspired by inspired by @kobik 's answer on , How to detect when the form is being maximized?.

WM_WINDOWPOSCHANGING will be called independed from MainFormOnTaskbar with different signatures on Message.WindowPos^.flags and respond on WIN + M too.

procedure TForm3.WMWindowPosChanging(var Message: TWMWindowPosChanging);
const
  Hide1=(SWP_NOCOPYBITS or SWP_SHOWWINDOW or SWP_FRAMECHANGED or SWP_NOACTIVATE);
  Hide2=((SWP_HIDEWINDOW or SWP_NOACTIVATE or SWP_NOZORDER or SWP_NOMOVE or SWP_NOSIZE));
begin
  inherited;
  if ((Message.WindowPos^.flags AND Hide1)  = Hide1)
         or ((Message.WindowPos^.flags AND Hide2)  = Hide2)  then
  begin
      Memo1.Lines.Add('Window got minimized');
  end;
end;

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 613441

Listen for WM_SIZE notification messages with a wParam parameter of SIZE_MINIMIZED.

Upvotes: 2

Related Questions