WinForm program start does not display the main form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.