c# wpf单实例运行并激活前实例

来源:互联网 发布:mac微信无法退出 编辑:程序博客网 时间:2024/05/14 09:08

先创建程序设置:

Properties——Settings.settings

名称类型范围值wlong用户0

   

 

在App.g.cs文件中加入:

using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using xxxxxxxxxxxxxxxxxxxxxxx.Properties;//项目的名称

 

#region Win32 API 函数

 

        //该函数设置由不同线程产生的窗口的显示状态;

        //如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。

        [DllImport("user32.dll")]

        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

 

        //该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。

        //如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。

        [DllImport("user32.dll")]

        private static extern bool SetForegroundWindow(IntPtr hWnd);

 

        // IsIconicIsZoomed ------ 分别判断窗口是否已最小化、最大化

        [DllImport("user32.dll")]

        private static extern bool IsIconic(IntPtr hWnd);

        [DllImport("user32.dll")]

        private static extern bool IsZoomed(IntPtr hWnd);

 

        //获取当前系统中被激活的窗口

        [DllImport("user32.dll")]

        private static extern IntPtr GetForegroundWindow();

 

        [DllImport("user32.dll")]

        private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);

 

        #endregion

 

        private const int SW_SHOWNORMAL = 1;

        private const int SW_RESTORE = 9;

 

 

Main方法:

 

[System.STAThreadAttribute()]

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]

        public static void Main()

        {

            bool b;

            string a = "1";     //名字任意          

            using (Mutex m = new Mutex(true, a, out b))

            {

                if (b)

                {

                    m.ReleaseMutex();

 

                    App app = new App();

                    app.InitializeComponent();

                    app.Run();

                }

                else

                {

                    Settings s = new Settings();

                    IntPtr i = (IntPtr)s.w;

 

                   

                    SetForegroundWindow(i);

                   

                   

                    if (IsIconic(i))

                    {

                        ShowWindowAsync(i, SW_RESTORE);

                    }

                    else

                    {

                        ShowWindowAsync(i, SW_SHOWNORMAL);

                    }

                   

                    GetForegroundWindow();

                    FlashWindow(i, true);

                }

            }

        }

 

在App.xaml.cs中:

using xxxxxx.Properties;

 

namespace xxxxxxxxxxx

{

     public partial class App: System.Windows.Application

     {       

        protected override void OnExit(ExitEventArgs e)

        {

            Settings r = new Settings();

            r.Reset();

           

            base.OnExit(e);

        }

    }

}

 

在Window1.xaml.cs中:(加入Loaded事件)

 

using xxxxxxxxxxxxxxxxxxxx.Properties;

using System.Windows.Interop;

 

private void Window_Loaded(object sender, RoutedEventArgs e)

        {

           

            IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(this)).Handle;

            Settings r = new Settings();

            r.w = (Int64)hwnd;

            r.Save();

           

        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击