Hide form when application is minimized

I have a basic form and a state form that I show when work is in progress in my application. If the work is finished, I simply call Hide in the status form, and the status form disappears.

My problem arises when I collapse the main form while the kind of expectation is visible. Then both forms are hidden, which is what I want. However, if the work ends while the main form is minimized, then when I restore it, the state form is also restored, even if Hide was invoked on it while minimized.

Visible looks like False for the state form when the application is minimized, and therefore the Hide call seems inefficient (help says that it just sets Visible to False ).

Are these observations correct? How is form visibility restored when the application gets focus again? How can I hide my form while the application is minimized?

+7
source share
3 answers

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

ShowOwnedPopups code calls with the first 'False' as' bShow 'while the application is minimizing, and then with' True 'as' b Show while the application is being restored. Since the function shows all the windows that were hidden by the previous call, changing the visibility of the form between them has no effect.

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

if the popup is hidden using ShowWindow functions, subsequently calling ShowOwnedPopups using fShow the parameter set to TRUE does not call the displayed window

Thus, one of the solutions could be to hide the form before the application hides it, so it will not be displayed during recovery. But then we need to know whether the displayed form should really be hidden or shown during recovery. This can be achieved by placing the property in a display form or using a global variable. In the following, “ShouldBeVisible” is a hypothetical property that will return true if we want 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; 
+4
source

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

If you do not need the same form object for life during the entire life of the application (which, most likely, is not), you can try to disable the automatic creation of a pop-up form (Project / Options) and then create and show it

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

and then free it on

 Form2.Release; 

Thus, the form cannot be restored together with the main form.

+2
source

Now I am using the following solution, which works for me:

  • In the Application.OnRestore recovery event handler, I call StatusForm.NotifyRestored . The status form is clearly hidden if it should not be visible.
  • In my status form, I track visibility in the FShouldDisplay boolean field. This is set in the 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; 
+2
source

All Articles