Detect all situations where form is minimized

隐身守侯 提交于 2019-12-08 13:13:24

问题


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?


回答1:


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.




回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/20195547/detect-all-situations-where-form-is-minimized

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