Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HwndHost for Windows Form - Win32 / WinForm Interoperability

I need to host a Win32 window in a Windows Form control. I had the same problem with WPF and I solved this problem by using the HwndHost control.

I followed this tutorial:

Walkthrough: Hosting a Win32 Control in WPF

Is there any equivalent control in Windows Form?

I have a Panel and its Handle property, I use this handle as parent of my Direct2D render target window:

// 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);

The code works if I use the HwndHost parent handle with WPF. But it does not render anything if I use the System.Windows.Forms.Panel Handle.

like image 504
Nick Avatar asked Apr 16 '15 15:04

Nick


2 Answers

What you had to do in WPF to create a valid D2D1 target window is not necessary in Winforms. It was necessary in WPF because controls are not windows themselves and don't have a Handle property, the one that CreateHwndRenderTarget() needs.

In Winforms the Panel class is already a perfectly good render target, you can use its Handle property to tell D2D1 where to render. You just need to tell it to stop painting itself. Add a new class to your project and paste this code:

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
            //...
        }
    }
}

Compile. Drop the new control onto your form, replacing the existing panel.

like image 173
Hans Passant Avatar answered Nov 15 '22 22:11

Hans Passant


Sounds like an MDI application. Something like this?

[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);
like image 40
Loathing Avatar answered Nov 15 '22 23:11

Loathing