zoukankan      html  css  js  c++  java
  • WinForm 登录窗体 + 单实例运行

    关于怎么在winform里增加登录窗体或者如何让winform程序单实例运行网上都很多例子。

    然而两者结合起来呢?

    //Program.cs

      static class Program
        {
            public static EventWaitHandle ProgramStarted;
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                bool createNew;
                ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MSFTD_SERVICE", out createNew);
    
                if (!createNew)
                {
                    ProgramStarted.Set();
                    return;
                }
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    
                try
                {
                    LoginForm LoginForm = new LoginForm();
                    if (LoginForm.ShowDialog() == DialogResult.OK)
                    {
                        Application.Run(new MainForm());
                    }
                }
                catch (Exception ex)
                {
                    //TODO
                }
            }
    
            static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                //TODO
            }
    
            static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                //TODO
            }
        }

    //LoginForm.cs

    public partial class LoginForm : Form
        {
            RegisteredWaitHandle handle;
    
            public LoginForm()
            {
                InitializeComponent();
                handle = ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
            }
    
            void OnProgramStarted(object state, bool timeout)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                });
            }

         
    private void btn_Login_Click(object sender, EventArgs e) {
           handle.Unregister(null);
      
           this.DialogResult = DialogResult.OK;

    }
        }

    //MainForm.cs

    public partial class MainForm : Form
        {
            #region init method, when user try to run two exe at the same time, show tips
            public MainForm()
            {
                InitializeComponent();
    
                this.SizeChanged += OnSizeChanged;
                ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
                this.WindowState = FormWindowState.Normal;
            }
    
            void OnSizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.notifyIcon.ShowBalloonTip(2000, "MSFTD Service", "MSFTD Service is running.", ToolTipIcon.Info);
                    this.Visible = false;
                }
            }
    
            private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }
    
            void OnProgramStarted(object state, bool timeout)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.notifyIcon.ShowBalloonTip(2000, "MSFTD Service", "MSFTD Service is running.", ToolTipIcon.Info);
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                });
            }
            #endregion
        }
  • 相关阅读:
    SQL关闭连接,及分离数据库
    C# 用户选择单个压缩-系统自带压缩
    设计模式7大原则--01
    httpclient
    Mac idea 常用命令
    集合整理
    几种简单的排序算法
    基础数据结构简介
    git 常用命令3
    git 常用命令2
  • 原文地址:https://www.cnblogs.com/AlvinLiang/p/4976600.html
Copyright © 2011-2022 走看看