Windows Forms: MouseWheel Capture

I have a windows form (works in C # .NET).

The form has a couple of panels at the top and some ComboBoxes and DataGridViews at the bottom.

I want to use scroll events on the top panels, but if you select, for example, the ComboBox is lost. Panels contain various other controls.

How can I always get mouse wheel events when the mouse is over any of the panels? So far, I have been trying to use MouseEnter / MouseEnter events, but no luck.

+6
winforms mousewheel
source share
2 answers

What you are describing is similar to what you want to reproduce functionality, such as Microsoft Outlook, where you do not need to click to focus the control in order to use the mouse wheel on it.

This is a rather difficult task to solve: it includes the implementation of the IMessageFilter interface of the contained form, the search for WM_MOUSEWHEEL events WM_MOUSEWHEEL and their direction to the control that the mouse is over.

Here's an example (from here ):

 using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsApplication1 { public partial class Form1 : Form, IMessageFilter { public Form1() { InitializeComponent(); Application.AddMessageFilter(this); } public bool PreFilterMessage(ref Message m) { if (m.Msg == 0x20a) { // WM_MOUSEWHEEL, find the control at screen position m.LParam Point pos = new Point(m.LParam.ToInt32()); IntPtr hWnd = WindowFromPoint(pos); if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) { SendMessage(hWnd, m.Msg, m.WParam, m.LParam); return true; } } return false; } // P/Invoke declarations [DllImport("user32.dll")] private static extern IntPtr WindowFromPoint(Point pt); [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); } } 

Please note that this code is active for all forms in your application, and not just for the main form.

+13
source share

Each control has a mousewheel event that occurs when the mouse wheel moves when the control has focus.

Check this out for more information: Control.MouseWheel Event

0
source share

All Articles