02-19-2021, 10:27 AM
I am not familiar with VB.Net but I see that you are missing the part that subscribes to calls AddMessageFilter and RemoveMessageFilter:
The "private MouseWheelMessageFilter(FrameworkElement element)" line create a private constructor for the MouseWheelMessageFilter class. This means that the MouseWheelMessageFilter class can be created only from the public static method RegisterMouseWheelHandling.
You can also change that so that you have a standard constructor that takes the FrameworkElement as a parameter.
The important thing (that is missing from your code) is that you need to subscribe element (as FrameworkElement) Loaded and Unloaded event. In the Loaded event you call System.Windows.Forms.Application.AddMessageFilter and pass an instance of MouseWheelMessageFilter object as a parameter. In the Unloaded event you call RemoveMessageFilter method.
Code:
public static void RegisterMouseWheelHandling(FrameworkElement element)
{
var mouseWheelMessageFilter = new MouseWheelMessageFilter(element);
}
private MouseWheelMessageFilter(FrameworkElement element)
{
_element = element;
_element.Loaded += delegate(object sender, RoutedEventArgs args)
{
System.Windows.Forms.Application.AddMessageFilter(this);
};
_element.Unloaded += delegate(object sender, RoutedEventArgs args)
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
};
}The "private MouseWheelMessageFilter(FrameworkElement element)" line create a private constructor for the MouseWheelMessageFilter class. This means that the MouseWheelMessageFilter class can be created only from the public static method RegisterMouseWheelHandling.
You can also change that so that you have a standard constructor that takes the FrameworkElement as a parameter.
The important thing (that is missing from your code) is that you need to subscribe element (as FrameworkElement) Loaded and Unloaded event. In the Loaded event you call System.Windows.Forms.Application.AddMessageFilter and pass an instance of MouseWheelMessageFilter object as a parameter. In the Unloaded event you call RemoveMessageFilter method.
Andrej Benedik

