我如何知道 WinForms ListView 滚动条何时到达底部?
发生这种情况时,我希望列表视图填充更多数据(在我的情况下理论上是无穷无尽的)。
OnScroll 事件为我提供了从顶部开始的滚动值,但我无法知道用户是否可以进一步滚动。
我使用来自伟大的 ObjectListView 代码项目的一些代码找到了答案: http://www.codeproject.com/KB/list/ObjectListView.aspx
调用 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;
}
Run Code Online (Sandbox Code Playgroud)
使用这个数据结构:
[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;
}
Run Code Online (Sandbox Code Playgroud)
nMax 给出了包括滚动句柄本身在内的总最大滚动值,因此实际有用的最大值是 nMax - nPage,其中 nPage 是滚动句柄的大小。
这很好用!