【问题标题】:.net 4.5 mouse event of wpf WindowsFormsHost hosting a windows form controlwpf WindowsFormsHost 的 .net 4.5 鼠标事件托管 Windows 窗体控件
【发布时间】:2013-12-04 17:58:20
【问题描述】:

我在.net4.5 wpf中的wpf WindowsFormsHost中托管了一个windows窗体控件,比如

<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>

aa_MouseClick 正确触发,但 WindowsFormsHost_MouseDown 和 Grid_MouseDown 从未触发(Windows 窗体控件吃掉事件),我该如何解决这个问题?

【问题讨论】:

    标签: wpf windowsformshost


    【解决方案1】:

    WPFWinForms 的事件处理技巧完全不同。 MouseDown 在 Winforms 中处理的事件无法传播到 WPF 控件。 WPF uses routed event instead of old event handling of WinForms.

    甚至事件处理程序的签名也不同。你可以做的是在WinForms control 上挂钩MouseDown 事件并且可以do your stuff from there

    您可以尝试提到的方法here,其他方法虽然从 WPF 到 Winforms,但也可以应用于您的情况。

    【讨论】:

    • 不一样,我知道,但是有什么方法可以将事件消息(窗口消息)直接传递给 wpf 而不能传递给 WindowsFormsHost 中的 winforms?
    • 不,没有这种方法。正如我在另一种方法中提到的,您需要在 WinForms 中创建自定义事件并在 WPF 中挂钩到该事件。从 WinForms 处理程序中引发该事件并在 WPF 中侦听。
    • 谢谢,有一个HwndHost,基本上我可以用它来达到同样的目的,如果重写WndProc不处理窗口消息,wpf可以接收事件
    • 是的,这也可以。完成后,请发布您的答案。很想看看它.. :)
    【解决方案2】:

    我的目的是创建一个窗口 hwnd 来渲染一些东西,但不关心事件 这是我现在使用的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多