我正在製作一個WinForms應用程序,並將ListView設置爲詳細信息,以便可以顯示多列。C#ListView沒有焦點的鼠標滾輪滾動
我希望此列表在鼠標移過控件並且用戶使用鼠標滾輪時滾動。現在,滾動僅在ListView具有焦點時發生。
即使它沒有焦點,我如何使ListView滾動?
我正在製作一個WinForms應用程序,並將ListView設置爲詳細信息,以便可以顯示多列。C#ListView沒有焦點的鼠標滾輪滾動
我希望此列表在鼠標移過控件並且用戶使用鼠標滾輪時滾動。現在,滾動僅在ListView具有焦點時發生。
即使它沒有焦點,我如何使ListView滾動?
通常,只有當鼠標/鍵盤事件具有焦點時,才能將鼠標/鍵盤事件發送到窗口或控件。如果你想看到他們沒有焦點,那麼你將不得不建立一個較低級別的鉤子。
「簡單」 和工作方案:
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
}