代码之家  ›  专栏  ›  技术社区  ›  Brian Gillespie

C列表查看无焦点鼠标滚轮滚动

  •  11
  • Brian Gillespie  · 技术社区  · 16 年前

    我正在制作一个将ListView设置为Detail的WinForms应用程序,以便显示多个列。

    我想让这个列表在鼠标悬停在控件上并且用户使用鼠标滚轮时滚动。现在,滚动仅在ListView具有焦点时发生。

    如何使ListView滚动,即使它没有焦点?

    2 回复  |  直到 16 年前
        1
  •  3
  •   Ray Hayes    16 年前

    通常只有当窗口或控件有焦点时,才会将鼠标/键盘事件发送到该窗口或控件。如果你想在没有焦点的情况下看到它们,那么你就必须放置一个较低水平的钩子。

    Here is an example low level mouse hook

        2
  •  6
  •   Chris W    10 年前

    “简单”和工作解决方案:

    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
    }