在使用RichTextBox的方法ScrollToCaret我需要知道,如果滚动条到达顶部/底部边缘。
When using the richtextbox method "ScrollToCaret" I need to know if the scrollbar reached the top/bottom margin.
这是因为,当那么如果我再次使用ScrollToCaret的方法垂直滚动条充满滚动至底部,那么它会产生一个奇怪的视觉效果,在控制因为它试图重新尝试向下滚动,但那里有没有更多的滚动,我不明白这个奇怪的逻辑RichTextBox控件。
This is because when vertical scrollbar is full scrolled to bottom then if I use again the "ScrollToCaret" method then it produces a weird visual effect in the control 'cause it try and retry to scroll down but theres nothing more to scroll, I can't understand this weird logic of richtextbox control.
我希望你能理解我,原谅我的英语水平。
I hope you could understand me, forgive my English.
PS:我使用的是默认的richtextbox垂直滚动条
PS: I'm using the default richtextbox vertical scrollbar.
您必须处理一个小的Win32
。该的win32
方法 GetScrollInfo
是我们所需要的。随着我们可以得到的最大范围,拇指与页
尺寸的当前位置(也就是拇指
尺寸)。因此,我们有以下公式:
You have to deal with a little Win32
. The win32
method GetScrollInfo
is what we need. With that we can get the maximum range, the current position of the thumb and the Page
size (which is the thumb
size). So we have this formula:
最高位置=最大范围 - 拇指大小
现在它的$ C $下您:
Now it's the code for you:
//Must add using System.Runtime.InteropServices;
//We can define some 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();
//...
}