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

c# wpf单实例运行并激活前实例 收藏
先创建程序设置:

Properties——Settings.settings

名称 类型 范围 值
w long 用户 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);

 

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

        [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();

           

        }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Zeroswl/archive/2009/10/29/4743992.aspx

你可能感兴趣的:(object,String,C#,user,Class,WPF)