第495篇--Five ways to hide a WinForm main Form

自古美人都是妖i 提交于 2019-11-26 21:37:28

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();
        }
    }
}

 

转载于:https://www.cnblogs.com/shanghaijimzhou/archive/2013/01/23/2873773.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!