Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseWheel event in WindowsFormsHost

Tags:

c#

winforms

wpf

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?

like image 827
David Michaeli Avatar asked Jan 18 '23 03:01

David Michaeli


1 Answers

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)

like image 71
Klaus78 Avatar answered Jan 26 '23 03:01

Klaus78