使用richtextbox 方法“ScrollToCaret”时,我需要知道滚动条是否到达顶部/底部边距。
这是因为当垂直滚动条完全滚动到底部时,如果我再次使用“ScrollToCaret”方法,它会在控件中产生奇怪的视觉效果,因为它会尝试并重试向下滚动,但没有更多可以滚动的东西,我可以'不明白richtextbox控件的这种奇怪逻辑。
我希望你能理解我,原谅我的英语。
PS:我使用的是默认的richtextbox 垂直滚动条。
使用richtextbox 方法“ScrollToCaret”时,我需要知道滚动条是否到达顶部/底部边距。
这是因为当垂直滚动条完全滚动到底部时,如果我再次使用“ScrollToCaret”方法,它会在控件中产生奇怪的视觉效果,因为它会尝试并重试向下滚动,但没有更多可以滚动的东西,我可以'不明白richtextbox控件的这种奇怪逻辑。
我希望你能理解我,原谅我的英语。
PS:我使用的是默认的richtextbox 垂直滚动条。
您必须稍微处理一下“Win32”。
“win32”中的“GetScrollInfo”方法就是你要找的。
使用“GetScrollInfo”方法,您可以获得以下 RichTextBox 滚动属性:
Max Position = Max Range - Thumb size
请参见下面的示例:
// Add the reference:
// using System.Runtime.InteropServices;
// Define an extension method for this purpose:
public static class RichTextBoxExtension
{
[DllImport("user32")]
private static extern int GetScrollInfo(IntPtr hwnd, int nBar,
ref SCROLLINFO scrollInfo);
public struct SCROLLINFO
{
public int cbSize;
public int fMask;
public int min;
public int max;
public int nPage;
public int nPos;
public int nTrackPos;
}
public static bool ReachedBottom(this RichTextBox rtb)
{
SCROLLINFO scrollInfo = new SCROLLINFO();
scrollInfo.cbSize = Marshal.SizeOf(scrollInfo);
//SIF_RANGE = 0x1, SIF_TRACKPOS = 0x10, SIF_PAGE= 0x2
scrollInfo.fMask = 0x10 | 0x1 | 0x2;
GetScrollInfo(rtb.Handle, 1, ref scrollInfo);//nBar = 1 -> VScrollbar
return scrollInfo.max == scrollInfo.nTrackPos + scrollInfo.nPage;
}
}
//Usage:
if (!yourRichTextBox.ReachedBottom())
{
yourRichTextBox.ScrollToCaret();
//...
}
这是@King King 版本,我已将其翻译为 VB.NET,还添加了更多功能,我认为它们都可以正常工作:
Public Class ScrollBarInfo
<System.Runtime.InteropServices.DllImport("user32")> _
Private Shared Function GetScrollInfo(hwnd As IntPtr, nBar As Integer, ByRef scrollInfo As SCROLLINFO) As Integer
End Function
Private Shared scrollInf As New SCROLLINFO()
Private Structure SCROLLINFO
Public cbSize As Integer
Public fMask As Integer
Public min As Integer
Public max As Integer
Public nPage As Integer
Public nPos As Integer
Public nTrackPos As Integer
End Structure
Private Shared Sub Get_ScrollInfo(control As Control)
scrollInf = New SCROLLINFO()
scrollInf.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(scrollInf)
scrollInf.fMask = &H10 Or &H1 Or &H2
GetScrollInfo(control.Handle, 1, scrollInf)
End Sub
Public Shared Function ReachedBottom(control As Control) As Boolean
Get_ScrollInfo(control)
Return scrollInf.max = scrollInf.nTrackPos + scrollInf.nPage
End Function
Public Shared Function ReachedTop(control As Control) As Boolean
Get_ScrollInfo(control)
Return scrollInf.nTrackPos < 0
End Function
Public Shared Function IsAtBottom(control As Control) As Boolean
Get_ScrollInfo(control)
Return scrollInf.max = (scrollInf.nTrackPos + scrollInf.nPage) - 1
End Function
Public Shared Function IsAtTop(control As Control) As Boolean
Get_ScrollInfo(control)
Return scrollInf.nTrackPos = 0
End Function
End Class