如何检测滚动条是否在最底部?

How to detect whether scrollbar is at the very bottom?

检测一个TScrollBox的垂直滚动条是否在最上面很容易:

IsScrollBarAtTop := ScrollBox1.VertScrollBar.Position = 0;

但是如何检测TScrollBox的垂直滚动条是否在最底部?

来自Vcl.Forms.TControlScrollBar.Range

Range represents the virtual size (in pixels) of the associated control's client area. For example, if the Range of a form's horizontal scroll bar is set to 500, and the width of the form is 200, the scroll bar's Position can vary from 0 to 300.

IsScrollBarAtBottom :=  ScrollBox1.VertScrollBar.Position =
  (ScrollBox1.VertScrollBar.Range - ScrollBox1.ClientHeight);

如果范围小于滚动框的高度,则滚动条不可见。

可以通过API获取滚动条信息,判断是否在底部。

function IsScrollBarAtBottom(Box: TScrollBox): Boolean;
var
  Info: TScrollInfo;
begin
  Info.cbSize := SizeOf(Info);
  Info.fMask := SIF_POS or SIF_RANGE or SIF_PAGE;
  Win32Check(GetScrollInfo(Box.Handle, SB_VERT, Info));
  Result := Info.nPos >=  Info.nMax - Info.nMin - Info.nPage;
end;