App.xaml.cs
1 protected override void OnStartup(StartupEventArgs e) 2 { 3 //禁止二次启动 4 this.Startup += new StartupEventHandler(App_Startup); 5 base.OnStartup(e); 6 } 7 #region 禁止二次启动 8 // 用于激活已打开的窗体 9 [DllImport("user32.dll")] 10 public static extern void SetForegroundWindow(IntPtr hwnd); 11 //操作当前窗体 12 //nCmdShow: 0关闭窗口/1正常大小显示窗口/2最小化窗口/3最大化窗口 13 [DllImport("user32.dll")] 14 public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); 15 16 System.Threading.Mutex mutex; 17 void App_Startup(object sender, StartupEventArgs e) 18 { 19 bool ret; 20 mutex = new System.Threading.Mutex(true, "WpfDemo", out ret); 21 if (!ret) 22 { 23 System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("WpfDemo"); 24 foreach (System.Diagnostics.Process p in proc) 25 { 26 SetForegroundWindow(p.MainWindowHandle); 27 ShowWindow(p.MainWindowHandle, 1); 28 NoSecond.SendMessage(p.MainWindowHandle); 29 } 30 Environment.Exit(0); 31 } 32 } 33 #endregion
Window.cs
#region 禁止二次启动 private void Window_SourceInitialized(object sender, EventArgs e) { HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); if (source == null) { throw new Exception("Cannot get HwndSource instance."); } source.AddHook(new HwndSourceHook(this.WndProc)); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { try { switch (msg) { case NoSecond.NoSecond_DATA: this.Show(); break; } } catch (Exception) { } return IntPtr.Zero; } #endregion
NoSecond.cs
1 #region 禁止二次启动程序 2 public class NoSecond 3 { 4 public const int NoSecond_DATA = 0x004A; 5 [DllImport("User32.dll", EntryPoint = "SendMessage")] 6 private static extern int SendMessage 7 ( 8 IntPtr hWnd, //目标窗体句柄 9 int Msg, //WM_COPYDATA 10 int wParam, //自定义数值 11 ref CopyDataStruct lParam //结构体 12 ); 13 [StructLayout(LayoutKind.Sequential)] 14 public struct CopyDataStruct 15 { 16 public IntPtr dwData; 17 public int cbData;//字符串长度 18 [MarshalAs(UnmanagedType.LPStr)] 19 public string lpData;//字符串 20 } 21 22 public static void SendMessage(IntPtr thisIntPtr) 23 { 24 IntPtr xmlIntPt = new IntPtr(GetIntPtr()); 25 if (xmlIntPt != IntPtr.Zero || thisIntPtr != IntPtr.Zero) 26 { 27 CopyDataStruct cds; 28 cds.dwData = IntPtr.Zero; 29 cds.lpData = "0123"; 30 cds.cbData = 4;//注意:长度为字节数 31 int fromWindowHandler = 0;// 消息来源窗体 32 SendMessage(thisIntPtr.ToInt32() == 0 ? xmlIntPt : thisIntPtr, NoSecond_DATA, fromWindowHandler, ref cds); 33 } 34 } 35 36 #region 获取窗体句柄指针 37 static string _IntPtrData = AppDomain.CurrentDomain.BaseDirectory + "/XML/Data.xml"; 38 static int GetIntPtr() 39 { 40 if (!System.IO.File.Exists(_IntPtrData)) 41 { 42 if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "/XML/")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "/XML/"); } 43 XmlDocument doc = new XmlDocument(); 44 doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "")); 45 XmlElement root = doc.CreateElement("Root"); 46 root.InnerText = "0"; 47 doc.AppendChild(root); 48 doc.Save(_IntPtrData); 49 } 50 XDocument xml = XDocument.Load(_IntPtrData); 51 XElement xmlRoot = xml.Root; 52 return Convert.ToInt32(xmlRoot.Value); 53 } 54 #endregion 55 } 56 #endregion
时间: 2024-12-28 23:42:19