Make tabpage not process mousewheel events (C#)
-
20-09-2019 - |
Question
I have made a MDI (tabbed) application that uses PictureBoxes inside TabPages. The picturebox is sometimes larger then the tabpage, so scrollbars appear. It is written in C# using Windows Forms.
Inside my tabpage, I capture and process mouse wheel events in the MouseWheel event (i use it to rotate some objects I draw in the picturebox).
But when I have the scrollbars, when I rotate the mouse wheel, my objects rotate, but the tabpage also scrolls down.
How can I make the tabpage not process the mousewheel event, and thus make it not scroll down? I want it to only be scrollable if the user clicks and drags on the scrollbar.
Solution
Subclass TabPage
and override the WndProc()
method to ignore the WM_MOUSEWHEEL
message:
public class MyTabPage : TabPage
{
private const int WM_MOUSEWHEEL = 0x20a;
protected override void WndProc(ref Message m)
{
// ignore WM_MOUSEWHEEL events
if (m.Msg == WM_MOUSEWHEEL)
{
return;
}
base.WndProc(ref m);
}
}
Then use your MyTabPage
subclass in place of the standard TabPage
.