欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

学习打卡 WPF应用创建单实例窗口

程序员文章站 2022-03-07 13:24:48
...

不懂,不会,看不明白,做个记录,以后用得到

····public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Mutex mutex = new Mutex(true, "单实例程序", out bool isNewInstance);
            if (isNewInstance!= true) {
                //MessageBox.Show("程序已启动");
                IntPtr intPtr = FindWindowW(null, "单实例窗口");
                if (intPtr != IntPtr.Zero) {
                    SetForegroundWindow(intPtr);
                }
                Shutdown();
            }
        }
        [DllImport("User32", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindowW(String lpClassName, String lpWindowName);

        [DllImport("User32", CharSet = CharSet.Unicode)]
        static extern Boolean SetForegroundWindow(IntPtr hWnd);
    }