我想在我的 WPF 应用程序中托管一个外部进程的窗口。我是HwndHost
这样得出的:
class HwndHostEx : HwndHost
{
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private IntPtr ChildHandle = IntPtr.Zero;
public HwndHostEx(IntPtr handle)
{
this.ChildHandle = handle;
}
protected override System.Runtime.InteropServices.HandleRef BuildWindowCore(System.Runtime.InteropServices.HandleRef hwndParent)
{
HandleRef href = new HandleRef();
if (ChildHandle != IntPtr.Zero)
{
SetParent(this.ChildHandle, hwndParent.Handle);
href = new HandleRef(this, this.ChildHandle);
}
return href;
}
protected override void DestroyWindowCore(System.Runtime.InteropServices.HandleRef hwnd)
{
}
}
并像这样使用它:
HwndHostEx host = new HwndHostEx(handle);
this.PART_Host.Child = host;
handle
我想托管的外部窗口的句柄在哪里,并且PART_Host
是我的 WPF 窗口内的边框:
<StackPanel UseLayoutRounding="True"
SnapsToDevicePixels="True"
Background="Transparent">
<Border Name="PART_Host" />
...
这给了我一个例外:
Hosted HWND must be a child window.
抱歉,我缺乏知识,但是在 WPF 应用程序中托管外部窗口的正确方法是什么?