Mutex mutex = null;
[DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string sClass, string sWindow);
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0; const int SW_SHOWNORMAL = 1; const int SW_RESTORE = 9; const int SW_SHOW = 5;
private void Application_Startup(object sender, StartupEventArgs e) { bool createdNew = false; mutex = new Mutex(true, "SPCAssist", out createdNew); if (!createdNew) { IntPtr hWndPtr = FindWindow(null, "SPCAssist"); //移动到最前 SwitchToThisWindow(hWndPtr, true); //s2;显示窗体 ShowWindow(hWndPtr, SW_SHOW| SW_RESTORE); //s3: 置顶 SetForegroundWindow(hWndPtr); Shutdown(); } ... }
|