2013-08-06 40 views
2

我有這個按鈕點擊代碼在Form1中:如何將工藝窗口帶到前面?

private void DriverVerifier_Click(object sender, EventArgs e) 
     { 

      if (MessageBox.Show("Are you Sure you want to Launch the Driver Verifier. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) 
      { 

      } 
      else 
      { 
       ProcessRun.Processing(Environment.SystemDirectory, "verifier.exe", "", false, ""); 
      } 

     } 

這將運行驅動程序驗證程序管理器程序來與Windows 7和8等版本。

問題是在某些Windows版本中某些用戶在驅動程序驗證程序管理器在窗體背後運行它時,無法將其帶到前面。

我的問題是,如果有什麼辦法強制這個過程在前面?

這是我的課ProcessRun的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Diagnostics; 
using System.IO; 

namespace Diagnostic_Tool_Blue_Screen 
{ 
    static class ProcessRun 
    { 

     public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName) 
     { 
      Process proc = new Process(); 
      proc.EnableRaisingEvents = true; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.RedirectStandardOutput = StandardOutput; 
      proc.StartInfo.FileName = FileName; 
      proc.StartInfo.CreateNoWindow = true; 
      proc.StartInfo.WorkingDirectory = WorkingDirectory; 
      proc.StartInfo.Arguments = Arguments; 
      proc.Start(); 
      if (StandardOutput == true) 
      { 
       string output = proc.StandardOutput.ReadToEnd(); 
       DumpOutput(WorkingDirectory + "\\" + OutputFileName, output); 
      } 
      proc.WaitForExit(); 
      proc.Close(); 
     } 

     private static void DumpOutput(string filename, string output) 
     { 
      StreamWriter w = new StreamWriter(filename); 
      w.Write(output); 
      w.Close(); 
     } 
    } 
} 

這是程序看起來怎麼樣的圖像時,其後臺形式的背後:

enter image description here 這是一個用戶與問題寫入描述問題:

發現一個錯誤,雖然同時使用Windows 8和Windows 8.1,還沒有測試與七:驅動程序驗證是相當無用的補充,因爲當你c舔它打開(底部的紅色/棕色按鈕),該工具的主窗口拒絕移動,最小化或關閉,保持焦點,所以無法看到驅動程序驗證程序窗口,因爲它仍然在工具窗口下方(見截圖)。

這只是一個較大的顯示屏和多顯示系統的小問題,因爲驅動程序驗證程序可以移到旁邊以便完全看到,但在較小的單顯示系統上,它使得該工具中的整個驅動程序驗證程序無用。

回答

4

我想這可能工作:

[DllImport("user32")] 
private static extern bool SetForegroundWindow(IntPtr hwnd); 
public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName) 
    { 
     Process proc = new Process(); 
     proc.EnableRaisingEvents = true; 
     proc.StartInfo.UseShellExecute = false; 
     proc.StartInfo.RedirectStandardOutput = StandardOutput; 
     proc.StartInfo.FileName = FileName; 
     proc.StartInfo.CreateNoWindow = true; 
     proc.StartInfo.WorkingDirectory = WorkingDirectory; 
     proc.StartInfo.Arguments = Arguments; 
     proc.Start(); 
     //Added code 
     System.Threading.Thread.Sleep(500); 
     SetForegroundWindow(proc.MainWindowHandle); 
     //........................................ 
     if (StandardOutput == true) 
     { 
      string output = proc.StandardOutput.ReadToEnd(); 
      DumpOutput(WorkingDirectory + "\\" + OutputFileName, output); 
     } 
     proc.WaitForExit(); 
     proc.Close(); 
    } 

如果你希望它是Topmost,使用SetWindowPos

[DllImport("user32")] 
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwnd2, int x, int y, int cx, int cy, int flags); 
//instead of calling SetForegroundWindow 
SetWindowPos(proc.MainWindowHandle, new IntPtr(-1), 0,0,0,0, 0x1 | 0x2); 
//SWP_NOSIZE = 1, SWP_NOMOVE = 2 -> keep the current pos and size (ignore x,y,cx,cy). 
//the second param = -1 -> set window as Topmost.