【发布时间】:2012-05-27 21:06:18
【问题描述】:
请帮助我,如何在 WPF 窗口中启动外部 .exe 应用程序。
下面的代码,我可以在 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...
}
}
}
【问题讨论】:
标签: wpf winforms windowsformshost windowsformsintegration