WindowsFormsHost中的WIndows Forms Chart不接收鼠标滚轮?

Mar*_*ser 5 forms wpf charts mousewheel windowsformshost

我在WindowsFormsHost中有一个Forms.DataVisualization.Charting.Chart.我无法让图表接收鼠标滚轮事件.点击工作正常,如果我尝试使用Forms.TextBox鼠标滚轮也正常工作.如果我在"原生"表单应用程序中使用图表,鼠标滚轮也可以正常工作.

那么,问题的关键是formHost中表单图表的组合.

这是一个简单的简单应用程序来复制问题:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <TextBlock Name="TextBlock1" Grid.Column="1" />
    <WindowsFormsHost Name="WindowsFormsHost1" Grid.Column="0"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

和背后的代码:

public MainWindow()
    {
        InitializeComponent();

        var chart = new Chart() { BackColor = System.Drawing.Color.Aquamarine};
        WindowsFormsHost1.Child = chart;

        chart.MouseDown += (a, b) => TextBlock1.Text += "FORMS click\r\n";
        TextBlock1.MouseDown += (a, b) => TextBlock1.Text += "WPF click\r\n";

        chart.MouseWheel += (a, b) => TextBlock1.Text += "FORMS wheel\r\n";
        TextBlock1.MouseWheel += (a, b) => TextBlock1.Text += "WPF wheel\r\n";
    }
Run Code Online (Sandbox Code Playgroud)

我可以从wpf接收所有点击和鼠标滚轮,但没有来自表单的轮子.我也试过了formHost的轮子监听器,没有成功.

有任何想法吗?Jon Skeet?

Eri*_*ono 1

这是 Windows 窗体 (WinForms) 和 WPF 之间的常见互操作性问题。WPF 事件的行为有所不同,它们使用路由事件而不是 Windows 窗体的旧事件处理。

WinForms 本身是 Win32 世界的消息处理映射,因此在 WPF 上嵌入 WindowsForms 控件时必须手动对其进行编码。为了处理鼠标单击以外的鼠标事件(包括鼠标滚轮),您必须代理 winforms 事件。

.NET 4的MSDN库明确提到了这一点:

代理 Windows 窗体消息循环

默认情况下,System.Windows.Forms.Application 类包含 Windows 窗体应用程序的主消息循环。在互操作期间,Windows 窗体消息循环不处理消息。因此,这个逻辑必须重现。ComponentDispatcher.ThreadFilterMessage 事件的处理程序执行以下步骤:

1.使用IMessageFilter接口过滤消息。

2.调用Control.PreProcessMessage方法。

3. 如果需要,翻译并发送消息。

4. 如果没有其他控件处理该消息,则将消息传递给宿主控件。

当窗口句柄被销毁时,WindowsFormsHost 控件将从注册中删除自身。

欲了解更多信息,请访问: http://msdn.microsoft.com/en-us/library/ms742474.aspx