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