2012-05-27 44 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名称;