#region 让程序以单一实例运行
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
/// <summary> /// 在 main() 中调用:if (!Common.RunSingleInstance()) return; /// </summary> /// <returns></returns> public static bool RunSingleInstance() { string proc = Process.GetCurrentProcess().ProcessName; Process[] processes = Process.GetProcessesByName(proc); if (processes.Length > 1) { Process p = Process.GetCurrentProcess(); int n = 0; if (processes[0].Id == p.Id) { n = 1; } IntPtr hWnd = processes[n].MainWindowHandle; if (IsIconic(hWnd)) { ShowWindowAsync(hWnd,SW_RESTORE); } SetForegroundWindow(hWnd); return false; } return true; } #endregion
原文链接:https://www.f2er.com/javaschema/287072.html