保持鼠标事件从WindowsFormsHost上冒泡 [英] Keep Mouse Events bubbling from WindowsFormsHost on

查看:516
本文介绍了保持鼠标事件从WindowsFormsHost上冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF WindowsFormsHost中具有Winforms控件. Winforms控件是被动的,不能处理任何鼠标事件. 应该像往常一样从WPF可视树中最内部的WPF控件引发鼠标事件,因此应使用WindowsFormsHost(或下一个).但是根本没有触发任何事件.

I have Winforms control within a WPF WindowsFormsHost. The Winforms control is passive and must not handle any mouse event. Mouse events should be raised as usual from the most inner WPF control in the WPF visual tree, so the WindowsFormsHost (or the next one). But no event is triggered at all.

我应该如何配置WindowsFormsHost(Winforms控件)以实现此目的?

How should I configure the WindowsFormsHost, the Winforms control, in order to achieve this?

备注:KeyDown和KeyUp的行为符合预期.但是鼠标事件却没有,如以下Snoop屏幕截图所示:

Remark: KeyDown and KeyUp behave as expected. But Mouse Events don't, as illustrated by the following Snoop screenshot:

推荐答案

实际上,Winforms控件会为自己保留鼠标事件,并且不会将事件转发给它的主机.解决方案是订阅winforms MouseDown事件并以编程方式生成Routed事件.

Indeed the Winforms Control keeps the mouse event for himself and doesn't forward the event to its host. The solution is to subscribe to the winforms MouseDown event and generate programmatically the Routed Event.

我重写WindowsFormsHost如下所示:

I overrided the WindowsFormsHost as following and it rocks:

(备注:一种行为可能更灵活)

(remark: a behavior may be more flexible)

public class ExtendedWindowsFormsHost : WindowsFormsHost
{
    public ExtendedWindowsFormsHost()
    {
        ChildChanged += OnChildChanged;
    }

    private void OnChildChanged(object sender, ChildChangedEventArgs childChangedEventArgs)
    {
        var previousChild = childChangedEventArgs.PreviousChild as Control;            
        if (previousChild != null)
        {
            previousChild.MouseDown -= OnMouseDown;
        }
        if (Child != null)
        {
            Child.MouseDown += OnMouseDown;
        }
    }

    private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
    {
        MouseButton? wpfButton = ConvertToWpf(mouseEventArgs.Button);
        if (!wpfButton.HasValue)
            return;

        RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, wpfButton.Value)
        {
            RoutedEvent = Mouse.MouseDownEvent,
            Source = this,
        });
    }

    private MouseButton? ConvertToWpf(MouseButtons winformButton)
    {
        switch (winformButton)
        {
            case MouseButtons.Left:
                return MouseButton.Left;
            case MouseButtons.None:
                return null;
            case MouseButtons.Right:
                return MouseButton.Right;
            case MouseButtons.Middle:
                return MouseButton.Middle;
            case MouseButtons.XButton1:
                return MouseButton.XButton1;
            case MouseButtons.XButton2:
                return MouseButton.XButton2;
            default:
                throw new ArgumentOutOfRangeException("winformButton");
        }
    }
}

这篇关于保持鼠标事件从WindowsFormsHost上冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆