View Code
1 using System; 2 using System.Windows.Forms; 3 using System.Runtime.InteropServices; 4 using System.Threading; 5 6 7 namespace OneInstanceOnlyMethodOne 8 { 9 static class Program 10 { 11 12 #region DllImportAttribute 13 14 [DllImport( " user32.dll " , CharSet = CharSet.Unicode)] 15 static extern IntPtr FindWindow( string className, string windowText); 16 17 const int SW_HIDE = 0 ; 18 const int SW_RESTORE = 1 ; 19 const int SW_MAXIMIZE = 2 ; 20 const int SW_MINIMIZE = 3 ; 21 const int SW_SHOW = 4 ; 22 const int SW_SHOWDEFAULT = 5 ; 23 24 /// <summary> 25 /// 该函数设置指定窗口的显示状态。 26 /// </summary> 27 /// <param name="handle"></param> 28 /// <param name="flags"></param> 29 /// <returns></returns> 30 [DllImport( " user32.dll " , CharSet = CharSet.Unicode)] 31 static extern bool ShowWindow(IntPtr handle, int flags); 32 33 /// <summary> 34 /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。 35 /// 键盘输入转向该窗口,并为用户改各种可视的记号。 36 /// 系统给创建前台窗口的线程分配的权限稍高于其他线程。 37 /// </summary> 38 /// <param name="handle"></param> 39 /// <returns></returns> 40 [DllImport( " user32.dll " , CharSet = CharSet.Unicode)] 41 static extern bool SetForegroundWindow(IntPtr handle); 42 #endregion 43 44 /// <summary> 45 /// 应用程序的主入口点。 46 /// </summary> 47 [STAThread] 48 static void Main() 49 { 50 bool isCreated; // 互斥体名称须唯一。 51 using (Mutex newMutex = new Mutex( true , " _OneInstanceOnly_ " , out isCreated)) 52 { 53 if (isCreated) 54 { 55 Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; 56 Application.EnableVisualStyles(); 57 Application.SetCompatibleTextRenderingDefault( false ); 58 Application.Run( new MethodOneMainForm()); 59 newMutex.ReleaseMutex(); // 释放互斥体的所属权。 60 } 61 else 62 { 63 string text = string .Format( " “{0}”应用程序已经运行。 " , AppDomain.CurrentDomain.FriendlyName); 64 MessageBox.Show(text, " 系统提示! " , MessageBoxButtons.OK, MessageBoxIcon.Warning); 65 66 // 查找之前打开的实例 67 IntPtr handle = FindWindow( null , " MethodOneMainForm " ); 68 if (handle != IntPtr.Zero) 69 { 70 // ShowWindow(handle, 1); 71 ShowWindow(handle, SW_RESTORE); 72 SetForegroundWindow(handle); 73 } 74 } 75 } 76 } 77 } 78 }
还有另外一种方法:
View Code
1 using System; 2 using System.Runtime.InteropServices; 3 using System.Windows.Forms; 4 using System.Diagnostics; 5 6 namespace OneInstanceOnlyMethodTwo 7 { 8 static class Program 9 { 10 [DllImport( " user32.dll " , EntryPoint = " SetForegroundWindow " )] 11 public static extern int SetForegroundWindow(IntPtr hwnd); 12 [DllImport( " user32.dll " , EntryPoint = " SendMessage " )] 13 public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 14 public const int WM_SYSCOMMAND = 0x112 ; 15 public const int SC_RESTORE = 0xF120 ; 16 17 18 19 /// <summary> 20 /// 应用程序的主入口点。 21 /// </summary> 22 [STAThread] 23 static void Main() 24 { 25 26 Process cur = Process.GetCurrentProcess(); 27 foreach (Process p in Process.GetProcesses()) 28 { 29 if (p.Id == cur.Id) 30 continue ; 31 if (p.ProcessName == cur.ProcessName) 32 { 33 string text = string .Format( " “{0}”已经运行。 " , AppDomain.CurrentDomain.FriendlyName); 34 MessageBox.Show(text, " 系统提示! " , MessageBoxButtons.OK, MessageBoxIcon.Warning); 35 36 SetForegroundWindow(p.MainWindowHandle); 37 SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0 ); 38 return ; 39 } 40 } 41 Application.EnableVisualStyles(); 42 Application.SetCompatibleTextRenderingDefault( false ); 43 Application.Run( new MethodTwoMainForm()); 44 } 45 } 46 }