C# ListView mouse wheel scroll without focus

前端 未结 2 460
别跟我提以往
别跟我提以往 2021-01-11 11:39

I\'m making a WinForms app with a ListView set to detail so that several columns can be displayed.

I\'d like for this list to scroll when the mouse is over the contr

相关标签:
2条回答
  • 2021-01-11 11:52

    "Simple" and working solution:

    public class FormContainingListView : Form, IMessageFilter
    {
        public FormContainingListView()
        {
            // ...
            Application.AddMessageFilter(this);
        }
    
        #region mouse wheel without focus
    
        // 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);
    
        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() & 0xffff, m.LParam.ToInt32() >> 16);
                IntPtr hWnd = WindowFromPoint(pos);
                if (hWnd != IntPtr.Zero && hWnd != m.HWnd && System.Windows.Forms.Control.FromHandle(hWnd) != null)
                {
                    SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
                    return true;
                }
            }
            return false;
        }
    
        #endregion
    }
    
    0 讨论(0)
  • 2021-01-11 12:05

    You'll normally only get mouse/keyboard events to a window or control when it has focus. If you want to see them without focus then you're going to have to put in place a lower-level hook.

    Here is an example low level mouse hook

    0 讨论(0)
提交回复
热议问题