Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide form when application is minimized

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?

like image 586
jpfollenius Avatar asked Jun 09 '11 15:06

jpfollenius


3 Answers

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;
like image 127
Sertac Akyuz Avatar answered Nov 17 '22 23:11

Sertac Akyuz


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);
begin
StatusForm.NotifyRestored;
end;

procedure TStatusForm.NotifyRestored;
begin
if not FShouldDisplay then
  ShowWindow(Handle, SW_HIDE);
end;

procedure TStatusForm.ShowStatusForm;
begin
FShouldDisplay := True;
Show;
end;

procedure TStatusForm.HideStatusForm;
begin
FShouldDisplay := False;
Hide;
end;
like image 33
jpfollenius Avatar answered Nov 17 '22 23:11

jpfollenius


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.

like image 2
Andreas Rejbrand Avatar answered Nov 18 '22 01:11

Andreas Rejbrand