VB.Net自定义RichTextBox SetScrollPos [英] VB.Net Custome RichTextBox SetScrollPos

查看:142
本文介绍了VB.Net自定义RichTextBox SetScrollPos的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件继承了RichTextBox窗体.我发现了一些应该获取/设置滚动位置的属性(它们在某种程度上起作用).这是获取/设置垂直滚动位置的方法:

I have a control that inherits form RichTextBox. I found A couple of properties that are supposed to get/set the scroll position (and they work to some degree). Here is the one to get/set the vertical scroll position:

    <dllimport("user32.dll",> _
Public Shared Function GetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer
End Function

<dllimport("user32.dll")> _
Private Shared Function SetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal nPos As Integer, ByVal bRedraw As Boolean) As Integer
End Function

Private Const SB_VERT As Integer = &H1
Public Property VScrollPos() As Integer
    Get
        Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT)
    End Get
    Set(ByVal value As Integer)
        SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT, value, True)
    End Set
End Property


问题在于它们不刷新RichTextBox的内容.滚动条刷新到正确的位置,但是文本和其他所有内容都没有.我尝试了谷歌搜索.我还尝试了几个呼叫,包括.Refresh()Application.DoEvents().Invalidate(),但是到目前为止它们都还没有起作用.还有什么我可以尝试迫使整体重绘的东西吗?

谢谢!
Ethan


The problem is that they don''t refresh the content of the RichTextBox. The scroll bars refresh to their correct positions, but the text and everything else don''t. I have tried Googling. I have also tried several calls including .Refresh(), Application.DoEvents(), and .Invalidate(), but none of them have worked as of yet. Is there anything else I can try in order to force the whole thing to redraw?

Thanks!
Ethan

推荐答案

SetScrollPos就是这样做的.无法更新窗口.
您可以使用SendMessage()功能获取/设置滚动位置.这是互操作性

The SetScrollPos just does that. Wont update the window.
You can use the SendMessage() function to get/set scroll position. Here''s the interop

Private Declare Auto Function RtfScroll _
                Lib "user32.dll" Alias "SendMessage" ( _
                ByVal hWnd As IntPtr, _
                ByVal Msg As Integer, _
                ByVal wParam As IntPtr, _
                ByRef lParam As System.Drawing.Point) As Integer

Private Const WM_USER = &H400
Private Const EM_GETSCROLLPOS = WM_USER + 221
Private Const EM_SETSCROLLPOS = WM_USER + 222



例如,将滚动位置设置为x = 0,y = 100



Eg to set scroll position to x=0, y =100

RtfScroll(RichTextCtl.Handle, EM_SETSCROLLPOS, 0, New System.Drawing.Point(0, 100))



获取滚动位置



To get scroll position

Dim pt As New System.Drawing.Point()
RtfScroll(rt.Handle, EM_GETSCROLLPOS, 0, pt)



============================

还有一种更简单的方法,无需使用互操作即可实现.

为此,
1.通过设置SelectionStart属性的值并将SelectionLength设置为零,将光标放置在适当的位置. 2.调用ScrollToCaret()函数滚动到插入符号



==============================

There''s another simpler way to achieve this without using interop.

To do it,
1. Place the cursor at the appropriate position by setting the SelectionStart property''s value and setting the SelectionLength to zero.
2. call the ScrollToCaret() function to scroll to the caret


这篇关于VB.Net自定义RichTextBox SetScrollPos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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