When the WinForm program starts, does not display the main form implementation method mainly has the following 5 kinds, the fifth kind is simplest, and the effect is also good, the fourth method also is worth recommending.
The instance code is as follows:
//method 1/5: Hide a form without specifying any form as the main form//Note: Typically, in a program, you close the main form and you can close the application. //but in the absence of the main form, No. //you can only use Application.exit () to close the application. //the using ensures that the application is closed before closing the Mymainformusing(NewForm1 ()) {Application.Run ();};//Application.Run (New Form1 ());
//methods for hiding forms 2/5://Close the main window with close () to close the applicationprotected OverrideCreateParams createparams{Get{Hide (); return Base. CreateParams; }}
//ways to hide a form 3/5://This method still can't close the application with the Close main window, and you have to use Application.exit. protected Override voidSetVisibleCore (BOOLvalue) {Base. SetVisibleCore (false);}
//Note: Methods 2 and 3 use show as if there is no way to bring up the main window, rather depressed.
//ways to Hide forms 4/5 PART1/2: Recommended!!!
//ApplicationContext is essentially a connector between the application and the main form,//governs the interaction between the two. One of the most important is to be responsible for the main form//closes when the thread ends. That being the case, we just have to customize a applicationcontext as needed.Internal classhideonstartupapplicationcontext:applicationcontext{PrivateForm mainforminternal;//constructor, the main form is stored in the mainforminternal PublicHideonstartupapplicationcontext (Form mainform) { This. mainforminternal = MainForm;
This. mainforminternal.closed + =NewEventHandler (mainforminternal_closed); }
//When the main form is closed, exit the application voidMainforminternal_closed (Objectsender, EventArgs e) {application.exit (); }}//in Main, make the following changes:Hideonstartupapplicationcontext context =NewHideonstartupapplicationcontext (NewForm1 ()); Application.Run (context);
//ways to hide a form 5/5: Recommended!!! //in the constructor or directly set the form property This. ShowInTaskbar =false; This. WindowState = formwindowstate.minimized;
The program with NotifyIcon control together with the implementation of the program to minimize the tray, the effect is very good!
WinForm program start does not display the main form