Hide form when application is minimized

delphi, delphi-xe, windows

Solution

`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;

Problem

I have a main form and a status form that I display when work is going on in my application. If the work is finished I just call `Hide` on the status form and the status form disappears. My problem occurs when I minimize the main form whilst the wait form is visible. Then both forms are hidden which is what I want. However, if the work finishes whilst the main form is minimized then when I restore it, the status form is also restored, even though `Hide` has been called on it whilst minimized. `Visible` seems to be `False` for the status form when the application is minimized and therefore calling `Hide` seems to have no effect (the help says it just sets `Visible` to `False`). Are that observations correct? How is the form visibility restored when the application gets focus again? How can I hide my form while the application is minimized?

Original source