gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:36:42 24 4
gpt4 key购买 nike

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

所以,造成问题的原因是表单图表在 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 的轮监听器,但没有成功。

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

最佳答案

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

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

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

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.

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

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

关于wpf - WindowsFormsHost 中的 WIndows Forms Chart 不接收鼠标滚轮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7414117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com