2008-09-17 9 views
11

여러 개의 열을 표시 할 수 있도록 ListView를 설정하여 WinForms 앱을 만들고 있습니다.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 
} 
관련 문제