簡體   English   中英

C# ListView 鼠標滾輪無焦點滾動

[英]C# ListView mouse wheel scroll without focus

我正在制作一個將 ListView 設置為 detail 的 WinForms 應用程序,以便可以顯示多個列。

當鼠標懸停在控件上並且用戶使用鼠標滾輪時,我希望此列表滾動。 現在,滾動僅在 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
}

您通常只會在具有焦點時將鼠標/鍵盤事件發送到窗口或控件。 如果您想在沒有焦點的情況下看到它們,那么您將不得不放置一個較低級別的掛鈎。

這是一個低級鼠標鈎子的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM