我在互联网上搜索了很多这样的问题,但是我还没有看到真正的答案。
我有一个丰富的文本框控件,其中包含大量文本。它在这个控制中有一些法律信息。默认情况下,“接受”按钮被禁用。如果v-滚动条的位置在底部,我想检测滚动事件。如果它在底部,则启用按钮。
如何检测当前的v-滚动条位置?
谢谢!
编辑I使用WinForms (.Net 4.0)
发布于 2012-04-20 00:32:23
这应该能让你接近你想要的东西。这个类继承自RichTextBox,并使用一些参数来确定滚动位置。它添加了一个事件ScrolledToBottom,如果用户使用滚动条滚动或使用键盘,则会触发该事件。
public class RTFScrolledBottom : RichTextBox {
public event EventHandler ScrolledToBottom;
private const int WM_VSCROLL = 0x115;
private const int WM_MOUSEWHEEL = 0x20A;
private const int WM_USER = 0x400;
private const int SB_VERT = 1;
private const int EM_SETSCROLLPOS = WM_USER + 222;
private const int EM_GETSCROLLPOS = WM_USER + 221;
[DllImport("user32.dll")]
private static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, Int32 wMsg, Int32 wParam, ref Point lParam);
public bool IsAtMaxScroll() {
int minScroll;
int maxScroll;
GetScrollRange(this.Handle, SB_VERT, out minScroll, out maxScroll);
Point rtfPoint = Point.Empty;
SendMessage(this.Handle, EM_GETSCROLLPOS, 0, ref rtfPoint);
return (rtfPoint.Y + this.ClientSize.Height >= maxScroll);
}
protected virtual void OnScrolledToBottom(EventArgs e) {
if (ScrolledToBottom != null)
ScrolledToBottom(this, e);
}
protected override void OnKeyUp(KeyEventArgs e) {
if (IsAtMaxScroll())
OnScrolledToBottom(EventArgs.Empty);
base.OnKeyUp(e);
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL) {
if (IsAtMaxScroll())
OnScrolledToBottom(EventArgs.Empty);
}
base.WndProc(ref m);
}
}这样就可以使用它了:
public Form1() {
InitializeComponent();
rtfScrolledBottom1.ScrolledToBottom += rtfScrolledBottom1_ScrolledToBottom;
}
private void rtfScrolledBottom1_ScrolledToBottom(object sender, EventArgs e) {
acceptButton.Enabled = true;
}必要时调整。
发布于 2016-07-15 11:05:52
以下内容在我的解决方案中非常有效:
Point P = new Point(rtbDocument.Width, rtbDocument.Height);
int CharIndex = rtbDocument.GetCharIndexFromPosition(P);
if (rtbDocument.TextLength - 1 == CharIndex)
{
btnAccept.Enabled = true;
}发布于 2012-04-19 23:41:51
How to get scroll position for RichTextBox?的问题可能会有所帮助,请查看此函数
richTextBox1.GetPositionFromCharIndex(0);https://stackoverflow.com/questions/10237781
复制相似问题