在WPF中托管外部exe
嗨,我正在尝试在WPF应用程序中托管一个exe(记事本)。 请在此处查找代码:在WPF中托管外部exe,wpf,winforms,wpf-4.0,windowsformshost,Wpf,Winforms,Wpf 4.0,Windowsformshost,嗨,我正在尝试在WPF应用程序中托管一个exe(记事本)。 请在此处查找代码: public partial class MainWindow : Window { private Process _process; private System.Windows.Forms.Panel _panel; [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, in
public partial class MainWindow : Window
{
private Process _process;
private System.Windows.Forms.Panel _panel;
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
const string patran = "patran";
public MainWindow()
{
InitializeComponent();
_process = Process.Start("notepad.exe");
_panel = new System.Windows.Forms.Panel();
wfHost.Child = _panel;
var patranPanelHandle = _panel.Handle;
SetParent(_process.MainWindowHandle, patranPanelHandle);
// remove control box
int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
// resize embedded application & refresh
ResizeEmbeddedApp();
}
private void ResizeEmbeddedApp()
{
if (_process == null)
return;
SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)_panel.Width, (int)_panel.Height, SWP_NOZORDER | SWP_NOACTIVATE);
}
protected override Size MeasureOverride(Size availableSize)
{
Size size = base.MeasureOverride(availableSize);
ResizeEmbeddedApp();
return size;
}
}
同样的代码对于Winforms也非常好,所以我使用WinformsHost,然后添加了Winforms面板,如上所述。但结果并不像预期的那样。记事本超出了WPF应用程序的范围,父/子记事本无法正常工作
太没有答案了我想,你调用
SetParent
太快了。一旦父窗口和子窗口都准备好,您需要执行SetParent
。所以,您可以等待父窗口被加载,然后启动子进程。但是,在调用SetParent
之前,在子进程上调用WaitForInputIdle
下面是我尝试过的示例代码,它可以正常工作:
public partial class MainWindow : Window
{
private Process _process;
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
const string patran = "patran";
public MainWindow()
{
InitializeComponent();
Loaded += (s, e) => LaunchChildProcess();
}
private void LaunchChildProcess()
{
_process = Process.Start("notepad.exe");
_process.WaitForInputIdle();
var helper = new WindowInteropHelper(this);
SetParent(_process.MainWindowHandle, helper.Handle);
// remove control box
int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
// resize embedded application & refresh
ResizeEmbeddedApp();
}
private void ResizeEmbeddedApp()
{
if (_process == null)
return;
SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)ActualWidth, (int)ActualHeight, SWP_NOZORDER | SWP_NOACTIVATE);
}
protected override Size MeasureOverride(Size availableSize)
{
Size size = base.MeasureOverride(availableSize);
ResizeEmbeddedApp();
return size;
}
有关WaitForInputdle的更多信息,请参阅本页
更新
附加结果图像: