首页 > 解决方案 > 尝试在 Windows 窗体中修改滚轮的自定义事件

问题描述

当您滚动到它的底部时,我试图让我的 ListBox 自动加载更多项目。为此,我需要一个滚动事件。

默认的 listBox 没有滚动事件。我设法找到了一些代码,在其中创建了一个新类并从默认列表框继承了所有内容。我成功地添加了滚动事件。

当通过鼠标单击使用滚动条时,覆盖工作完美,但不幸的是,当使用滚轮时不会捕获事件。

我试过优化它,所以它也会拿起滚轮,但没有成功。

我想修改的类,所以它也会拿起滚轮。

using System;
using System.Windows.Forms;

public class BetterListBox : ListBox
{
    // Event declaration
    public delegate void BetterListBoxScrollDelegate(object Sender, BetterListBoxScrollArgs e);
    public event BetterListBoxScrollDelegate Scroll;
    // WM_VSCROLL message constants
    private const int WM_VSCROLL = 0x0115;
    private const int SB_THUMBTRACK = 5;
    private const int SB_ENDSCROLL = 8;

    protected override void WndProc(ref Message m)
    {
        // Trap the WM_VSCROLL message to generate the Scroll event
        base.WndProc(ref m);
        if (m.Msg == WM_VSCROLL)
        {
            int nfy = m.WParam.ToInt32() & 0xFFFF;
            if (Scroll != null && (nfy == SB_THUMBTRACK || nfy == SB_ENDSCROLL))
                Scroll(this, new BetterListBoxScrollArgs(this.TopIndex, nfy == SB_THUMBTRACK));
        }
    }
    public class BetterListBoxScrollArgs
    {
        // Scroll event argument
        private int mTop;
        private bool mTracking;
        public BetterListBoxScrollArgs(int top, bool tracking)
        {
            mTop = top;
            mTracking = tracking;
        }
        public int Top
        {
            get { return mTop; }
        }
        public bool Tracking
        {
            get { return mTracking; }
        }
    }
}

标签: c#winformsscrolllistbox

解决方案


推荐阅读