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 }