Search code examples
delphimdiminimizemdichildwm-syscommand

Catch Delphi MDI Child minimize event when the MDI Child form is maximized


I am in need of some help trying to capture the minimize event of an MDI Child form when it is maximized.

I am able to capture the minimize/restore/maximize events when the form is not maximized when clicking the buttons circled in red in the image below.

MDI Child

I capture the aforementioned events by using WMSysCommand:

procedure TMDIChildForm.WMSysCommand(var Msg: TWMSysCommand);
begin
  if Msg.CmdType = SC_MINIMIZE then
  begin
    //my code here
  end;
end;

When I try to capture the same events using WMSysCommand when the MDI Child form is maximized and clicking the buttons circled in red in the image below, it will not call this code.

Maximized MDI Child

No matter what I have tried I have been unsuccessful in catching these events. If someone could point me in the right direction it would be greatly appreciated. Thank you.


Solution

  • Works fine for me when I try it:

    type
      TMDIChildForm = class(TForm)
      private
        { Private declarations }
        procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
      public
        { Public declarations }
      end;
    
    procedure TMDIChildForm.WMSysCommand(var Msg: TWMSysCommand);
    begin
      inherited; // <-- ADD THIS!!
      if Msg.CmdType = SC_MINIMIZE then
      begin
        // code here
      end;
    end;
    

    WMSysCommand() does capture the SC_MINIMIZE notification whenever the MDI child is minimized, regardless of whether it was previously maximized or not, as expected.

    Make sure TMDIChildForm.WMSysCommand() calls inherited (as shown above) to pass the WM_SYSCOMMAND message to the default handler so Windows has a chance to process it.