6

我想在我的 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 应用程序中托管外部窗口的正确方法是什么?

4

3 回答 3

5

在调用“SetParent”之前,请执行以下操作:

public const int GWL_STYLE = (-16);
public const int WS_CHILD = 0x40000000;

SetWindowLong(this.ChildHandle, GWL_STYLE, WS_CHILD);
于 2016-01-28T17:23:26.743 回答
1

查看文档

  1. 您需要为 CLI 编译的 Win32 控件
  2. 您必须在 WPF 应用程序中创建控件。

从 WPF 应用程序中的另一个进程“附加”一个已经运行的窗口似乎是不可能的。

如何获得传递给 HwndHostEx 构造函数的句柄?

于 2015-05-12T10:53:00.153 回答
0

为了增加说明,这是在 WPF 应用程序中托管进程的最终代码,取自 Jose 的回答:

    class HwndHostEx : HwndHost
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);

        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)
            {
                const int GWL_STYLE = (-16);
                const int WS_CHILD = 0x40000000;

                SetWindowLong(this.ChildHandle, GWL_STYLE, WS_CHILD);


                SetParent(this.ChildHandle, hwndParent.Handle);
                href = new HandleRef(this, this.ChildHandle);
            }

            return href;
        }

        protected override void DestroyWindowCore(System.Runtime.InteropServices.HandleRef hwnd)
        {

        }
    }

// to create an instance:
var processName = "Whatever.exe";
var process = System.Diagnostics.Process.GetProcesses()
 .FirstOrDefault(item => item.ProcessName.ToLowerInvariant() == processName && item.MainWindowHandle != IntPtr.Zero);
var handle = process?.MainWindowHandle;

if(handle != null)
{
    var host = new HwndHostEx(handle.Value);

    YourControl.Grid.Children.Add(host);
}


于 2021-09-30T17:32:44.867 回答