3

我的问题是:如何检测滚动条是否位于富文本框的末尾?

编辑:当我最后说时,我的意思是完全滚动到底部,而不是其他任何地方。

4

4 回答 4

2

查看GetScrollRangeGetScrollPosAPI ...

Private Const SBS_HORZ = 0
Private Const SBS_VERT = 1

<DllImport("user32.dll")> _
Public Function GetScrollRange(ByVal hWnd As IntPtr, ByVal nBar As Integer, _
                               ByRef lpMinPos As Integer, _
                               ByRef lpMaxPos As Integer) As Boolean
End Function

<DllImport("user32.dll")> _
Public Function GetScrollPos(ByVal hWnd As Integer, _
                             ByVal nBar As Integer) As Integer
End Function

// ...

Dim scrollMin as Integer = 0
Dim scrollMax as Integer = 0

If(GetScrollRange(rtb.Handle, SBS_VERT, scrollMin, scrollMax) Then
   Dim pos as Integer = GetScrollPos(rtb.Handle, SBS_VERT)

   // Detect if they're at the bottom
EndIf

笔记:

要确定滚动条是否可见,请调用GetWindowLong并检查WS_VSCROLL

要确定滑块可以达到的最大值,请调用GetScrollInfo;我认为最大值是

scrollMax - largeChange + 1
于 2009-03-18T03:18:18.123 回答
2

我使用此代码正确获取当前和最大位置:

    const int SB_HORZ          = 0x0000;
    const int SB_VERT          = 0x0001;
    const int WM_HSCROLL       = 0x0114;
    const int WM_VSCROLL       = 0x0115;
    const int SB_THUMBPOSITION = 4;

    private enum ScrollInfoMask : uint
    {
        SIF_RANGE           = 0x1,
        SIF_PAGE            = 0x2,
        SIF_POS             = 0x4,
        SIF_DISABLENOSCROLL = 0x8,
        SIF_TRACKPOS        = 0x10,
        SIF_ALL             = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
    }

    [StructLayout(LayoutKind.Sequential)]
    struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int  nMin;
        public int  nMax;
        public uint nPage;
        public int  nPos;
        public int  nTrackPos;
    }

    public int HScrollPosition
    {
        get
        {
            return GetScrollPos(Handle, SB_HORZ);
        }
        set
        {
            SetScrollPos((IntPtr)Handle, SB_HORZ, value, true);
            PostMessageA((IntPtr)Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
        }
    }

    public int VScrollPosition
    {
        get
        {
            return GetScrollPos(Handle, SB_VERT);
        }
        set
        {
            SetScrollPos((IntPtr)Handle, SB_VERT, value, true);
            PostMessageA((IntPtr)Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
        }
    }

    public int HScrollPositionMax
    {
        get
        {
            SCROLLINFO scrollInfo = new SCROLLINFO();
            scrollInfo.fMask = (uint)ScrollInfoMask.SIF_ALL;
            scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
            GetScrollInfo(Handle, SB_HORZ, ref scrollInfo);

            return scrollInfo.nMax - (int)scrollInfo.nPage;
        }
    }

    public int VScrollPositionMax
    {
        get
        {
            SCROLLINFO scrollInfo = new SCROLLINFO();
            scrollInfo.fMask = (uint)ScrollInfoMask.SIF_ALL;
            scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
            GetScrollInfo(Handle, SB_VERT, ref scrollInfo);

            return scrollInfo.nMax - (int)scrollInfo.nPage;
        }
    }

获得“正确”的最大值很乏味。您必须减去 GetScrollRange 返回的最大值的 nPage(大变化)值,然后您可以正确地将其与 HScrollPosition 属性进行比较,例如:

if (_rtb.VScrollPosition == _rtb.VScrollPositionMax)
{
    Debug.WriteLine("Scroll is at the bottom most edge");
}
于 2013-02-04T13:18:33.387 回答
1
        if (richTextBox1.Size.Width - richTextBox1.ClientSize.Width > 10) 

抱歉,您必须自己将其转换为 VB.NET。不应该太难:)

于 2011-12-13T06:00:10.047 回答
0

一种方法是使用 GetScrollPos 函数。这是一个使用 VB.NET 的冗长示例。

http://www.codeproject.com/KB/vb/VbNetScrolling.aspx?print=true

于 2009-03-18T03:18:54.987 回答