MouseWheel event in WindowsFormsHost

2020-07-17 14:40发布

I have a WPF application that is using a WindowsFormsHost control to host a control of Windows.Forms.

I tried to implement the MouseWheel event - but it seems that the the MouseWheel event never fired.

Is there a workaround for this issue?

标签: c# wpf winforms
2条回答
【Aperson】
2楼-- · 2020-07-17 15:05

one thing im gonna add... if a child of WindowsFormsHost is a Windows.Forms element then this link helps.

http://vastpark-svn.cvsdude.com/public/trunk/src/Sample.Client/WPFInputSource.cs

why im posting is i was looking for a solution and found.. so i thought it might be helpful somebody in the future. anyway, thanks for asking here first^^

查看更多
\"骚年 ilove
3楼-- · 2020-07-17 15:15

A workaround is to use event MouseEnter.

Suppose you have a winform label in a WindowsFormHost

In XAML

<WindowsFormsHost Height="100" Name="windowsFormsHost1" Width="200" />

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

Now MouseWheel should work (label shoud change color)

查看更多
登录 后发表回答