检测 ListView 滚动条何时到达底部 [英] Detecting when ListView scrollbar reaches the bottom

查看:30
本文介绍了检测 ListView 滚动条何时到达底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何知道 WinForms ListView 滚动条何时到达底部?

How do I know when the WinForms ListView scrollbar reaches it's bottom?

发生这种情况时,我希望列表视图填充更多数据(在我的案例中理论上是无穷无尽的).

When this happens, I want the listview to be populated with more data (which is endless in theory in my case).

OnScroll 事件为我提供了从顶部开始的滚动值,但我无法知道用户是否可以进一步滚动.

The OnScroll event gives me the scroll value from the top, but I have no way of knowing if the user can scroll any further or not.

推荐答案

我使用伟大的 ObjectListView 代码项目中的一些代码找到了答案:http://www.codeproject.com/KB/list/ObjectListView.aspx

I found an answer using some code from the great ObjectListView code-project: http://www.codeproject.com/KB/list/ObjectListView.aspx

调用 GetScrollInfo:

call GetScrollInfo:

    private const int SIF_RANGE = 0x0001;
    private const int SIF_PAGE = 0x0002;
    private const int SIF_POS = 0x0004;
    private const int SIF_DISABLENOSCROLL = 0x0008;
    private const int SIF_TRACKPOS = 0x0010;
    private const int SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS);        
    private const int SB_HORZ = 0;
    private const int SB_VERT = 1;

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, SCROLLINFO scrollInfo);

    public static SCROLLINFO GetFullScrollInfo(ListView lv, bool horizontalBar) {
      int fnBar = (horizontalBar ? SB_HORZ : SB_VERT);

      SCROLLINFO scrollInfo = new SCROLLINFO();
      scrollInfo.fMask = SIF_ALL;
      if (GetScrollInfo(lv.Handle, fnBar, scrollInfo))
        return scrollInfo;
      else
        return null;
    }

使用这个数据结构:

    [StructLayout(LayoutKind.Sequential)]
    public class SCROLLINFO
    {
        public int cbSize = Marshal.SizeOf(typeof(SCROLLINFO));
        public int fMask;
        public int nMin;
        public int nMax;
        public int nPage;
        public int nPos;
        public int nTrackPos;
    }

nMax 给出了包括滚动句柄本身在内的总最大滚动值,因此实际有用的最大值是 nMax - nPage,其中 nPage 是滚动句柄的大小.

the nMax gives the total max scroll value including the scroll handle itself, so the actually useful max value is nMax - nPage, where nPage is the size of the scroll handle.

这很好用!

这篇关于检测 ListView 滚动条何时到达底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆