WindowsFormsHost 中的 WIndows Forms Chart 不接收鼠标滚轮?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Windows 窗体 (WinForms) 和 WPF 之间的常见互操作性问题。 WPF 事件的行为有所不同,它们使用路由事件而不是 Windows 窗体的旧事件处理。
WinForms 本身是 Win32 世界的消息处理映射,因此在 WPF 上嵌入 WindowsForms 控件时必须手动对其进行编码。为了处理鼠标单击以外的鼠标事件(包括鼠标滚轮),您必须代理 winforms 事件。
.NET 4的MSDN库明确提到了这一点:
当窗口句柄被销毁时,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:
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