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

发布于 2024-12-04 12:17:42 字数 1207 浏览 0 评论 0原文

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

因此,造成问题的原因是 formsHost 中表单图表的组合。

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

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

    <TextBlock Name="TextBlock1" Grid.Column="1" />
    <WindowsFormsHost Name="WindowsFormsHost1" Grid.Column="0"/>
</Grid>

以及背后的代码:

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

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

有什么想法吗?乔恩·斯基特?

i have a Forms.DataVisualization.Charting.Chart in a WindowsFormsHost. I can't get the chart to receive mouse wheel events. Clicks are working, if i try with a Forms.TextBox the mouse wheel is working, too. The mouse wheel is also working if i use the chart in a "native" forms app.

So, what makes the problem is the combination of the forms chart in a formsHost.

Here is a verry simple application to replicate the problem:

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

    <TextBlock Name="TextBlock1" Grid.Column="1" />
    <WindowsFormsHost Name="WindowsFormsHost1" Grid.Column="0"/>
</Grid>

and the code behind:

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

i can receive all clicks and the mouse wheel from wpf, but no wheel from forms. I tried the wheel listener of the formsHost as well, without success.

Any ideas? Jon Skeet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2024-12-11 12:17:42

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

This is a common interoperability problem between Windows Forms (WinForms) and WPF. WPF events behave differently, they use routed event instead of the old event handling of Windows Forms.

WinForms itself is a map of message handling from Win32 world, therefore you have to code it manually when embedding WindowsForms control on WPF. In order to handle mouse event other than mouse click (this including mouse wheel), you have to surrogate the winforms event.

MSDN library of .NET 4 mentioned this clearly:

Surrogate Windows Forms Message Loop

By default, the System.Windows.Forms.Application class contains the primary message
loop for Windows Forms applications. During interoperation, the
Windows Forms message loop does not process messages. Therefore, this
logic must be reproduced. The handler for the
ComponentDispatcher.ThreadFilterMessage event performs the following
steps:

1.Filters the message using the IMessageFilter interface.

2.Calls the Control.PreProcessMessage method.

3.Translates and dispatches the message, if it is required.

4.Passes the message to the hosting control, if no other controls
process the message.

When the window handle is destroyed, the WindowsFormsHost control removes itself from registration.

For more info, visit:
http://msdn.microsoft.com/en-us/library/ms742474.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文