C# ListView mouse wheel scroll without focus

前端 未结 2 475
别跟我提以往
别跟我提以往 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
    }
    

提交回复
热议问题