• 一个比较完整的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();
            }
        }
  • 相关阅读:
    Android Studio同时监听多个Button实现activity跳转
    Android Studio--Activity实现跳转功能
    Gym 101170F Free Weights(二分)题解
    Gym 101170I Iron and Coal(BFS + 思维)题解
    Gym 101128F Landscaping(网络流)题解
    Gym 101128A Promotions(思维 + dfs)题解
    Gym 101480I Ice Igloos(思维乱搞)题解
    Gym 101480F Frightful Formula(待定系数)题解
    Gym 101128J Saint John Festival(凸包 + 二分判点和凸包关系)题解
    CodeForces 348D Turtles(LGV定理)题解
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1699358.html
Copyright © 2020-2023  润新知