#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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。