2012-05-27 43 views
0

請幫我啓動外部的.exe程序,如何在WPF窗口啓動外部的.exe程序。如何在WPF窗口

下面的代碼,我能打開WPF窗口的Notepad.exe和WinWord.exe進程應用程序,但沒有其他應用程序..當我嘗試打開它在單獨的窗口中打開其他應用程序.exe文件。

public partial class Window1 : Window 
{ 
    public IntPtr MainWindowHandle { get; set; } 


    [DllImport("user32.dll", SetLastError = true)] 
    private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 


    //[DllImport("user32.dll", SetLastError = true)] 
    //private static extern int GetWindowLong(IntPtr hWnd, int nIndex); 

    public Window1() 
    { 
     InitializeComponent(); 

     try 
     { 
      //External exe inside WPF Window 
      System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel(); 

      WindowsFormsHost windowsFormsHost1 = new WindowsFormsHost(); 

      windowsFormsHost1.Child = _pnlSched; 

      _Grid.Children.Add(windowsFormsHost1); 

      //_Grid.Children.Add(_pnlSched); 

      ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files\Atwin\Atwin2k2.exe"); 

      psi.WindowStyle = ProcessWindowStyle.Normal; 

      Process PR = Process.Start(psi); 

      PR.WaitForInputIdle(); // true if the associated process has reached an idle state. 

      System.Threading.Thread.Sleep(3000); 

      IntPtr hwd = PR.MainWindowHandle; 
      SetParent(PR.MainWindowHandle, _pnlSched.Handle); // loading exe to the wpf window. 

     } 
     catch (Exception ex) 
     { 
      //Nothing... 
     } 
     } 



} 

回答

0

可能有幾件事可能會導致此行爲,這裏是兩個不同的東西,我剛纔遇到:

當我試圖用的vim.exe和第一次發生異常類型庫未註冊,所以我註冊並VIM.EXE加載成功後。這可能是您的應用程序的行爲。

當我試圖加載Eclipse和沒有例外,但爲的eclipse.exe加載WPF窗口之外。展望間諜++我發現了導致Windows之外WPF窗口打開這是形容這裏爲什麼WM_ACTIVATEAPP消息:

http://support.microsoft.com/kb/89563

所以取決於你想在你的WPF,應用程序打開不是什麼樣的應用每個應用程序都將打開,因爲應用程序特定的某些限制。

+0

感謝您的答覆...的應用程序,我試圖在WPF窗口中打開在大型機開發。 –

0

串strReportPath = System.IO.Directory.GetCurrentDirectory();

 if (strReportPath.Substring(strReportPath.Length - 9) == "bin\\Debug") 
     { 
      strReportPath = strReportPath.Substring(0, strReportPath.Length - 10); 
     } 

     Process p = new Process(); 
     p.StartInfo = new ProcessStartInfo(strReportPath + @"\bin\Debug\drag.exe"); 
     p.Start(); 

//拖動是你的sln名稱;