2008-09-17 96 views
11

我正在制作一个WinForms应用程序,并将ListView设置为详细信息,以便可以显示多列。C#ListView没有焦点的鼠标滚轮滚动

我希望此列表在鼠标移过控件并且用户使用鼠标滚轮时滚动。现在,滚动仅在ListView具有焦点时发生。

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

回答

3

通常,只有当鼠标/键盘事件具有焦点时,才能将鼠标/键盘事件发送到窗口或控件。如果你想看到他们没有焦点,那么你将不得不建立一个较低级别的钩子。

Here is an example low level mouse hook

5

“简单” 和工作方案:

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 
}