HwndHost for Windows Form - Win32 / WinForm互操作性

时间:2015-04-16 15:41:55

标签: c# c++ wpf winforms interop

我需要在Windows窗体控件中托管Win32窗口。我遇到了与WPF相同的问题,我使用HwndHost控件解决了这个问题。

我遵循了本教程:

Walkthrough: Hosting a Win32 Control in WPF

Windows窗体中是否有任何等效控件?

我有Panel及其Handle属性,我使用此句柄作为我的Direct2D渲染目标窗口的父级:

// Register the window class.
        WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };

        // Redraws the entire window if a movement or size adjustment changes the height 
        // or the width of the client area.
        wcex.style         = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc   = Core::WndProc;
        wcex.cbClsExtra    = 0;
        wcex.cbWndExtra    = sizeof(LONG_PTR);
        wcex.hInstance     = HINST_THISCOMPONENT;
        wcex.hbrBackground = nullptr;
        wcex.lpszMenuName  = nullptr;
        wcex.hCursor       = LoadCursor(nullptr, IDI_APPLICATION);
        wcex.lpszClassName = L"SVGCoreClassName";

        RegisterClassEx(&wcex);

hwnd = CreateWindow(
            L"SVGCoreClassName",        // class name
            L"",                        // window name
            WS_CHILD | WS_VISIBLE,      // style
            CW_USEDEFAULT,              // x
            CW_USEDEFAULT,              // y
            CW_USEDEFAULT,              // width
            CW_USEDEFAULT,              // height
            parent,                     // parent window
            nullptr,                    // window menu
            HINST_THISCOMPONENT,        // instance of the module to be associated with the window
            this);                      // pointer passed to the WM_CREATE message

...

hr = d2dFactory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(hwnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY),
        &renderTarget);

如果我在WPF中使用HwndHost父句柄,则代码可以正常工作。但是如果我使用System.Windows.Forms.Panel句柄,它就不会呈现任何内容。

2 个答案:

答案 0 :(得分:7)

在Winforms中,您无需在WPF中创建有效的D2D1目标窗口。在WPF中这是必要的,因为控件本身不是窗口,并且没有Handle属性,即CreateHwndRenderTarget()需要的属性。

在Winforms中,Panel类已经是一个非常好的渲染目标,你可以使用它的Handle属性来告诉D2D1渲染的位置。你只需要告诉它停止自己绘画。在项目中添加一个新类并粘贴此代码:

using System;
using System.Windows.Forms;

class D2D1RenderTarget : Control {
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (!this.DesignMode) {
            this.SetStyle(ControlStyles.UserPaint, false);
            // Initialize D2D1 here, use this.Handle
            //...
        }
    }
}

编译。将新控件拖放到表单上,替换现有面板。

答案 1 :(得分:0)

听起来像是MDI应用程序。像这样的东西?

[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll")]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

Form f = new Form();
Button b1 = new Button { Text = "Notepad" };
b1.Click += delegate {
    using (var p = Process.Start("notepad.exe")) {
        p.WaitForInputIdle();
        SetParent(p.MainWindowHandle, f.Handle);
        MoveWindow(p.MainWindowHandle, 50, 50, 300, 300, true);
    }
};
f.Controls.Add(b1);
Application.Run(f);
相关问题