一个比较完整的WindowsFormsApplication实现

这是前两天课堂上的例子,提供了一个自定义WindowsFormsApplication的范例,主要包括如下的功能

1. 单一实例

2. 闪屏

3. 登陆窗口

4. 任务栏图标和菜单

需要注意的是,WindowsFormsApplicationBase这个类是要添加Microsoft.VisualBisic.dll引用的

 

    class MyApplication : WindowsFormsApplicationBase

    {

        NotifyIcon taskBarIcon;

        public MyApplication(bool singleton)

            : base(AuthenticationMode.ApplicationDefined)

        {

            //设置单实例

            this.IsSingleInstance = true;



            

            //添加任务栏按钮

            taskBarIcon = new NotifyIcon();

            taskBarIcon.Icon = Properties.Resources.Northwind;

            ContextMenuStrip contextMenu = new ContextMenuStrip();

            contextMenu.Items.Add(

                new ToolStripMenuItem(

                    "退出",

                    Properties.Resources.close,

                    (sender, eventArgs) =>

                    {

                        if (MessageBox.Show(

                            "你是否真的要退出?",

                            "确认",

                            MessageBoxButtons.YesNo,

                            MessageBoxIcon.Question,

                            MessageBoxDefaultButton.Button2)

                            == DialogResult.Yes)

                        {

                            Application.Exit();

                        }

                    }));

            contextMenu.Items.Add(

                new ToolStripMenuItem(

                    "帮助",

                    Properties.Resources.help,

                    (sender, eventArgs) =>

                    {

                        MessageBox.Show("帮助文档还在制作中");

                    }));



            taskBarIcon.ContextMenuStrip = contextMenu;

            taskBarIcon.ShowBalloonTip(2000, "欢迎", "欢迎使用该软件", ToolTipIcon.Info);

            taskBarIcon.Visible = true;

        }



        public MyApplication() : this(true) { }



        protected override void OnCreateSplashScreen()

        {

            base.OnCreateSplashScreen();



            LoginForm login = new LoginForm();

            if (login.ShowDialog() != DialogResult.OK)

            {

                Environment.Exit(-1);//这里不能用Application.Exit,因为当前是一个自定义的Application

            }



            this.MinimumSplashScreenDisplayTime = 2000;//最少显示两秒

            this.SplashScreen = new SplashForm();



        }









        protected override void OnShutdown()

        {

            base.OnShutdown();

            taskBarIcon.Dispose();



        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)

        {

            base.OnStartupNextInstance(eventArgs);

            eventArgs.BringToForeground = true;

        }







        protected override void OnCreateMainForm()

        {

            base.OnCreateMainForm();



            this.MainForm = new MainForm();

        }

    }

你可能感兴趣的:(application)