.net 4.5 mouse event of wpf WindowsFormsHost hosting a windows form control

2.4k views Asked by At

I host a windows form control in wpf WindowsFormsHost in .net4.5 wpf, like

<Grid MouseDown="Grid_MouseDown" Background="#FFE85050">

    <WindowsFormsHost HorizontalAlignment="Left" Height="244"
                      VerticalAlignment="Top" Width="335" 
                      MouseDown="WindowsFormsHost_MouseDown">
        <ctrl:UserControl1 x:Name="aa" MouseClick="aa_MouseClick"/>
    </WindowsFormsHost>

</Grid>

the aa_MouseClick fires correctly but WindowsFormsHost_MouseDown and Grid_MouseDown never fires(the windows form control eat the event), how can I fix this problem?

2

There are 2 answers

0
BB Chung On

My purpose is create a window hwnd to render something, but not care about the event here is the solution that I am now using

public class RenderHwndHost : HwndHost
{
    public delegate void AfterSizeChangedHandler();

    internal const int
        WS_CHILD = 0x40000000,
        WS_VISIBLE = 0x10000000,
        LBS_NOTIFY = 0x00000001,
        HOST_ID = 0x00000002,
        LISTBOX_ID = 0x00000001,
        WS_VSCROLL = 0x00200000,
        WS_BORDER = 0x00800000;

    private IntPtr hwndHost;

    [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
    internal static extern bool DestroyWindow(IntPtr hwnd);

    [DllImport("user32.dll", EntryPoint = "CreateWindowEx", CharSet = CharSet.Unicode)]
    internal static extern IntPtr CreateWindowEx(int dwExStyle,
        string lpszClassName,
        string lpszWindowName,
        int style,
        int x, int y,
        int width, int height,
        IntPtr hwndParent,
        IntPtr hMenu,
        IntPtr hInst,
        [MarshalAs(UnmanagedType.AsAny)] object pvParam);

    protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        handled = false;
        return IntPtr.Zero;
    }

    protected override void DestroyWindowCore(HandleRef hwnd)
    {
        DestroyWindow(hwnd.Handle);
    }

    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
    {
        base.OnRenderSizeChanged(sizeInfo);
        if (AfterSizeChanged != null)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                new Action(() => AfterSizeChanged()));
        }
    }

    public event AfterSizeChangedHandler AfterSizeChanged;

    protected override HandleRef BuildWindowCore(HandleRef hwndParent)
    {
        hwndHost = CreateWindowEx(0, "static", "",
            WS_CHILD,
            0, 0,
            100, 100,
            hwndParent.Handle,
            (IntPtr) HOST_ID,
            IntPtr.Zero,
            0);


        return new HandleRef(this, hwndHost);
    }
4
Rohit Vats On

Event handling startegy of WPF and WinForms is completely different. MouseDown event handled in Winforms can't be propagated up to WPF control. WPF uses routed event instead of old event handling of WinForms.

Even event handlers differ in signature. What you can do is hook MouseDown event on WinForms control and can do your stuff from there.

OR

You can try approach mentioned here, its other way round though from WPF to Winforms but can be applied in your situation as well.