Hide form when application is minimized Hide form when application is minimized windows windows

Hide form when application is minimized


Visible of the display form is indeed false and calling Hide does nothing when the application is minimized, because it is hidden by the application as part of the minimization mechanism.

Code calls ShowOwnedPopups with first 'False' as 'bShow' while the application is minimizing, and then with 'True' as 'bShow' while the application is restoring. Since the function shows all windows which was hidden by a previous call, changing visibility of a form in between has no effect.

Now, see this quote from the remarks section of the documentation of the function,

if a pop-up window is hidden by using the ShowWindow function, subsequently calling ShowOwnedPopups with the fShow parameter set to TRUE does not cause the window to be shown

So, one solution can be to hide the form before the application hides it, so it won't get shown while restoring. But then we have to know if the display form is actually to be hidden or shown when we restore. This can be achieved by putting a property on the display form or with a global variable perhaps. In the below, 'ShouldBeVisible' is a hypothetical property that would return true if we are to display information:

type  TForm1 = class(TForm)  ..  private    procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;  ...procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);begin  if (Msg.CmdType = SC_MINIMIZE) and Assigned(Form2) and Form2.Visible then    Form2.Hide;  inherited;  if (Msg.CmdType = SC_RESTORE) and Assigned(Form2) and Form2.ShouldBeVisible then    Form2.Show;end;


I now use the following solution which works for me:

  1. In Application.OnRestore restore event handler I call StatusForm.NotifyRestored. Status form is explicitly hidden if it should not be visible.
  2. In my status form I keep track of visibility in a boolean field FShouldDisplay. This is set in methods ShowStatusForm and HideStatusForm.

procedure TMainForm.OnApplicationRestore(Sender : TObject);beginStatusForm.NotifyRestored;end;procedure TStatusForm.NotifyRestored;beginif not FShouldDisplay then  ShowWindow(Handle, SW_HIDE);end;procedure TStatusForm.ShowStatusForm;beginFShouldDisplay := True;Show;end;procedure TStatusForm.HideStatusForm;beginFShouldDisplay := False;Hide;end;


Warning: I am not 100 % sure that the following approach is safe.

If you don't need the same form object to be alive for the duration of the application's life (which you most likely do not), then you could try to disable the automatic creation of the popup form (Project/Options) and then create and show it by

Application.CreateForm(TForm2, Form2);Form2.Show;

and then free it by

Form2.Release;

This way the form cannot possibly be restored together with the main form.