none
RichTextBox 的滚动条问题 RRS feed

答案

  • Hi Steven,

    从你的描述中,我了解到你想通过VB.NET实现一个获取RichTextBox控件垂直滚动条已经滚动到最下方的功能。

    我采用这样一种方式来实现的,我重写了系统自带的RichTextBox控件,给这个控件增加了一个ScrolledToBottom这样的事件,用来监听是否垂直滚动条是否滚动到最下方。请看下面的实现代码,

    Imports System.Runtime.InteropServices
    
    Public Class RTFScrolledBottom
        Inherits RichTextBox
        Public Event ScrolledToBottom As EventHandler
    
        Private Const WM_VSCROLL As Integer = &H115
        Private Const WM_MOUSEWHEEL As Integer = &H20A
        Private Const WM_USER As Integer = &H400
        Private Const SB_VERT As Integer = 1
        Private Const EM_SETSCROLLPOS As Integer = WM_USER + 222
        Private Const EM_GETSCROLLPOS As Integer = WM_USER + 221
    
        <DllImport("user32.dll")> _
        Private Shared Function GetScrollRange(hWnd As IntPtr, nBar As Integer, ByRef lpMinPos As Integer, ByRef lpMaxPos As Integer) As Boolean
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(hWnd As IntPtr, wMsg As Int32, wParam As Int32, ByRef lParam As Point) As IntPtr
        End Function
    
        Public Function IsAtMaxScroll() As Boolean
            Dim minScroll As Integer
            Dim maxScroll As Integer
            GetScrollRange(Me.Handle, SB_VERT, minScroll, maxScroll)
            Dim rtfPoint As Point = Point.Empty
            SendMessage(Me.Handle, EM_GETSCROLLPOS, 0, rtfPoint)
    
            Return (rtfPoint.Y + Me.ClientSize.Height >= maxScroll)
        End Function
    
        Protected Overridable Sub OnScrolledToBottom(e As EventArgs)
            RaiseEvent ScrolledToBottom(Me, e)
        End Sub
    
        Protected Overrides Sub OnKeyUp(e As KeyEventArgs)
            If IsAtMaxScroll() Then
                OnScrolledToBottom(EventArgs.Empty)
            End If
    
            MyBase.OnKeyUp(e)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_VSCROLL OrElse m.Msg = WM_MOUSEWHEEL Then
                If IsAtMaxScroll() Then
                    OnScrolledToBottom(EventArgs.Empty)
                End If
            End If
    
            MyBase.WndProc(m)
        End Sub
    
        Private Sub InitializeComponent()
            Me.SuspendLayout()
            Me.ResumeLayout(False)
        End Sub
    End Class

    我创建了一个这样的例子供你参考,我在一个窗体上放置了重写后的RichTextBox控件,然后在后台代码中给这个重写后的控件Text属性赋值,一开始运行的状态是这样的,在RichTextBox下面有一个Button,这时候Button是不可用的。请看截图:

    当垂直滚动条滚动到最下方后,Button激活了。请看截图:

    另外我将这个程序的源代码上传到SkyDrive上面了,你可以从下面的地址进行下载。

    https://skydrive.live.com/redir?resid=F6BFF8BD172F0399!154

    这里有一些资料是关于这个Topic的,推荐给你吧。

    http://stackoverflow.com/questions/1619783/get-vertical-scroll-bar-position-as-integer-in-vb-net

    http://stackoverflow.com/questions/10237781/get-current-scroll-position-from-rich-text-box-control

    祝你中秋快乐!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月19日 5:48
    版主