-
Notifications
You must be signed in to change notification settings - Fork 186
Modern control scrolling
Wiki ▸ Modern control scrolling
In Windows Vista the guidelines for mouse wheel control scrolling changed. Previously using the mouse wheel would scroll the control which has focus at the time, regardless of where the actual mouse cursor is. Modern applications instead send the mouse wheel events to the control which the cursor is currently hovering over instead.
To emulate this behaviour in .NET WinForms there are various solutions available, with the best one probably being the usage of the IMessageFilter
interface.
DarkUI contains an instance of this implementation which will intercept all incoming mouse wheel events and re-route them to the control you're currently hovering over instead of the focused control.
This functionality is encapsulated in the ControlScrollFilter
class.
public class ControlScrollFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case (int)WM.MOUSEWHEEL:
case (int)WM.MOUSEHWHEEL:
var hControlUnderMouse = Native.WindowFromPoint(new Point((int)m.LParam));
if (hControlUnderMouse == m.HWnd)
return false;
Native.SendMessage(hControlUnderMouse, (uint)m.Msg, m.WParam, m.LParam);
return true;
}
return false;
}
}
To enable this functionality within your application, simply add the following line somewhere which is only going to be called once (preferably before the user is able to interact with any controls).
Application.AddMessageFilter(new ControlScrollFilter());
Personally I place this within the Program
class just before calling Application.Run()