本文介绍了WindowsFormsHost 中的 MouseWheel 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 WPF 应用程序,它使用 WindowsFormsHost 控件来承载 Windows.Forms 控件.
I have a WPF application that is using a WindowsFormsHost control to host a control of Windows.Forms.
我尝试实现 MouseWheel 事件 - 但似乎 MouseWheel 事件从未触发.
I tried to implement the MouseWheel event - but it seems that the the MouseWheel event never fired.
是否有解决此问题的方法?
Is there a workaround for this issue?
推荐答案
一种解决方法是使用事件 MouseEnter.
A workaround is to use event MouseEnter.
假设您在 WindowsFormHost 中有一个 winform 标签
Suppose you have a winform label in a WindowsFormHost
在 XAML 中
<WindowsFormsHost Height="100" Name="windowsFormsHost1" Width="200" />
在 C# 中
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
label.Text = "Hallo";`
label.MouseEnter += new EventHandler(label_MouseEnter);
label.MouseWheel += new System.Windows.Forms.MouseEventHandler(label_MouseWheel);
windowsFormsHost1.Child = label;
.....
void label_MouseEnter(object sender, EventArgs e)
{
(sender as System.Windows.Forms.Label).Focus();
}
void label_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
(sender as System.Windows.Forms.Label).BackColor = System.Drawing.Color.Red;
}
现在 MouseWheel 应该可以工作了(标签应该改变颜色)
Now MouseWheel should work (label shoud change color)
这篇关于WindowsFormsHost 中的 MouseWheel 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!