第495篇--Five ways to hide a WinForm main Form
Sometimes, you do not want the main WinForm form to show, so you can try the following five steps:
static class Fom1:Form { public Form1() { // 1 (The best way) this.ShowInTaskbar = false; this.WindowState = FormWindowState.Minimized; } //2 protected override CreateParams CreateParams { get { Hide(); return base.CreateParams; } } // 3 protected override void SetVisibleCore(bool value) { base.SetVisibleCore(false); } }
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace FormHideDemo { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); // 4 //using (new TestForm()) //{ // Application.Run(); //} // 5 //HideOnStartupApplicationContext context = new HideOnStartupApplicationContext(new TestForm()); //Application.Run(context); } } internal class HideOnStartupApplicationContext : ApplicationContext { private Form mainFormInternal; // 构造函数,主窗体被存储在mainFormInternal public HideOnStartupApplicationContext(Form mainForm) { this.mainFormInternal = mainForm; this.mainFormInternal.Closed += new EventHandler(mainFormInternal_Closed); } // 当主窗体被关闭时,退出应用程序 void mainFormInternal_Closed(object sender, EventArgs e) { Application.Exit(); } } }