从获得丰富的文本框控件当前滚动位置? [英] Get current scroll position from rich text box control?

查看:144
本文介绍了从获得丰富的文本框控件当前滚动位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查了互联网无远弗届,看到很多类似这样的问题,但我还没有看到实际的答案。

I have searched the internet far and wide and seen many questions like this, but I have not seen an actual answer.

我有很多在其文本格式文本框控件。它在该控制一些法律资料。默认情况下,接受按钮将被禁用。我想探测在滚动事件发生V型滚动条的位置是在底部。如果它是在底部,使按钮

I have a rich text box control with lots of text in it. It has some legal information in this control. By default the "Accept" button is disabled. I want to detect on the scroll event if the position of the v-scroll bar is at the bottom. If it is at the bottom, enable the button.

我怎么会检测当前的V-滚动条位置?

How would I detect the current v-scroll bar position?

感谢您!

修改 我使用的WinForms(.NET 4.0)

EDIT I am using WinForms (.Net 4.0)

推荐答案

这应该让你接近你所期待的。这个类继承自RichTextBox中,并使用一些pinvoking确定滚动位置。它增加了一个事件 ScrolledToBottom 这被炒鱿鱼,​​如果使用滚动条或使用键盘的用户滚动。

This should get you close to what you are looking for. This class inherits from the RichTextBox and uses some pinvoking to determine the scroll position. It adds an event ScrolledToBottom which gets fired if the user scrolls using the scrollbar or uses the keyboard.

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);
  }

}

这是那么它如何能习惯:

This is then how it can get used:

public Form1() {
  InitializeComponent();
  rtfScrolledBottom1.ScrolledToBottom += rtfScrolledBottom1_ScrolledToBottom;
}

private void rtfScrolledBottom1_ScrolledToBottom(object sender, EventArgs e) {
  acceptButton.Enabled = true;
}

扭捏必要

这篇关于从获得丰富的文本框控件当前滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆