1 /// 该函数设置由不同线程产生的窗口的显示状态 2 /// </summary> 3 /// <param name="hWnd">窗口句柄</param> 4 /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分</param> 5 /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零</returns> 6 [DllImport("User32.dll")] 7 private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); 8 /// <summary> 9 /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。 10 /// 系统给创建前台窗口的线程分配的权限稍高于其他线程。 11 /// </summary> 12 /// <param name="hWnd">将被激活并被调入前台的窗口句柄</param> 13 /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零</returns> 14 [DllImport("User32.dll")] 15 private static extern bool SetForegroundWindow(IntPtr hWnd); 16 17 private void BringToFront(IntPtr hWnd) 18 { 19 //Process currentProcess = Process.GetCurrentProcess(); 20 //Process process = Process.get(id); 21 //if (process != null) 22 //{ 23 // ShowWindowAsync(process.MainWindowHandle, 1);//显示 24 // SetForegroundWindow(process.MainWindowHandle);//当到最前端 25 //} 26 Process[] Processes = Process.GetProcesses();// GetProcessesByName(currentProcess.ProcessName); 27 foreach (Process process in Processes) 28 { 29 //process.Start(); 30 //if (process.Id != currentProcess.Id) 31 //{ 32 // if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") 33 //<span style="white-space:pre"> </span> == currentProcess.MainModule.FileName) 34 // { 35 // return process; 36 // } 37 if (process.MainWindowHandle.ToString() == "0") 38 { 39 continue; 40 } 41 if (process.MainWindowHandle == hWnd) 42 { 43 ShowWindowAsync(process.MainWindowHandle, 1);//显示 44 SetForegroundWindow(process.MainWindowHandle);//当到最前端 45 } 46 //} 47 } 48 }