日期:2014-05-18 浏览次数:21231 次
[DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll", EntryPoint = "FindWindow")] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //添加api //获取当前运行的实例 public static Process GetRunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) return process; } return null; } public static void HandleRunningInstance(Process instance) { if (instance != null && instance.MainWindowHandle.ToInt32() != 0) { string windowTile = instance.MainWindowTitle.Trim(); if (windowTile == "系统标题") ShowWindowAsync(instance.MainWindowHandle, 3); else ShowWindowAsync(instance.MainWindowHandle, 1); SetForegroundWindow(instance.MainWindowHandle); } else { IntPtr hwnd = FindWindow(null, "系统标题"); if (hwnd.ToInt32() == 0) { hwnd = FindWindow(null, "系统标题"); ShowWindowAsync(hwnd, 1); SetForegroundWindow(hwnd); } else { ShowWindowAsync(hwnd, 3); SetForegroundWindow(hwnd); } } }
------解决方案--------------------
http://topic.csdn.net/u/20091118/18/9f3f7c0a-20fa-4006-9ccd-823734b10353.html