在 vb.net 中获取垂直滚动条位置作为整数

发布于 2024-08-09 01:00:32 字数 138 浏览 3 评论 0原文

我有一个 RichTextBox,我需要找到垂直滚动条的位置。

没有 Pinvoke 有什么办法可以做到这一点吗?如果没有,有什么方法可以使用 Pinvoke 来做到这一点?

我需要返回一个整数值。

感谢您的帮助!

I have a RichTextBox and I need to find the position of the vertical scroll bar.

Is there any way to do this without Pinvoke? If not, what is a way to do this WITH Pinvoke?

I need to return an integer value.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-08-16 01:00:32

嗯,是的,Jay 的解决方案实现了承诺:它改变了滚动条的位置。它没有改变的是 RichTextBox 内容的位置。

这是我在 codeproject.com 找到的解决方案: https: //www.codeproject.com/Questions/293542/VB-Net-Custome-RichTextBox-SetScrollPos

Public Class Rtbscrollfix
    Inherits RichTextBox

    <DllImport("user32.dll")>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As System.Drawing.Point) As Integer
    End Function

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

    Public Property ScrollY() As Integer
        Get
            Dim pt As New System.Drawing.Point
            SendMessage(Me.Handle, EM_GETSCROLLPOS, 0, pt)
            Return pt.Y
        End Get
        Set(value As Integer)
            SendMessage(Me.Handle, EM_SETSCROLLPOS, 0, New System.Drawing.Point(0, value))
        End Set
    End Property

End Class

Well, yeah, Jay's solution does what promised: it changes the positions of scrollbars. What it doesn't change is the position of the CONTENT of the richtextbox.

Here is the solution I found over at codeproject.com: https://www.codeproject.com/Questions/293542/VB-Net-Custome-RichTextBox-SetScrollPos

Public Class Rtbscrollfix
    Inherits RichTextBox

    <DllImport("user32.dll")>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As System.Drawing.Point) As Integer
    End Function

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

    Public Property ScrollY() As Integer
        Get
            Dim pt As New System.Drawing.Point
            SendMessage(Me.Handle, EM_GETSCROLLPOS, 0, pt)
            Return pt.Y
        End Get
        Set(value As Integer)
            SendMessage(Me.Handle, EM_SETSCROLLPOS, 0, New System.Drawing.Point(0, value))
        End Set
    End Property

End Class
递刀给你 2024-08-16 01:00:32

如果没有 PInvoke,我不知道有什么方法可以做到这一点。您可以使用 PInvoke 调用 GetScrollPos

您可以尝试以下方法。

第 1 步:通过扩展标准 RichTextBox 创建自定义 RichTextBox 控件。

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace WindowsFormsApplication1

    Public Class MyRichTextBox
        Inherits RichTextBox
        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        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_HORZ As Integer = &H0
        Private Const SB_VERT As Integer = &H1

        ''' <summary>
        ''' Gets and Sets the Horizontal Scroll position of the control.
        ''' </summary>
        Public Property HScrollPos() As Integer
            Get
                Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ)
            End Get
            Set(ByVal value As Integer)
                SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ, value, True)
            End Set
        End Property

        ''' <summary>
        ''' Gets and Sets the Vertical Scroll position of the control.
        ''' </summary>
        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
    End Class
End Namespace

这将向标准 RichTextBox 添加两个属性:HScrollPos 和 VScrollPos。这些属性将允许您获取和设置控件中的水平和垂直滚动条位置。

第 2 步:创建一个测试表单并尝试您的控件。

在与自定义控件相同的项目中创建 Winform。将自定义控件拖放到测试窗体中,并在窗体上添加一个按钮。在窗体的 Click 事件中,添加以下代码以查看自定义控件的垂直滚动位置。

Console.WriteLine(myRichTextBox1.VScrollPos)

需要注意的一些事项:

  1. 如果您的控件当前未
    显示垂直滚动条时,
    调用 HScrollPos 会使你的
    程序。有几个明显的
    解决这个问题的方法(检查
    之前显示过滚动条
    检查财产,或作出
    垂直滚动条始终可见等)。

  2. 取决于控制方式(以及
    可能的形式)的大小(不
    提及文本内容的变化
    的控制),设置
    VScrollPos 可能会使您的程序崩溃或
    不将其恢复到原来的位置
    当 VScrollPos 值为
    保存。

  3. 我从来没有使用过这个代码。我
    我以为你的问题是
    有趣并且做了一点
    研究得出我的答案。

I don't know any way to do this without PInvoke. You can use PInvoke to call GetScrollPos.

Here's how you can try it.

Step 1: Create a custom RichTextBox control by extending a standard RichTextBox.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace WindowsFormsApplication1

    Public Class MyRichTextBox
        Inherits RichTextBox
        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        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_HORZ As Integer = &H0
        Private Const SB_VERT As Integer = &H1

        ''' <summary>
        ''' Gets and Sets the Horizontal Scroll position of the control.
        ''' </summary>
        Public Property HScrollPos() As Integer
            Get
                Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ)
            End Get
            Set(ByVal value As Integer)
                SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ, value, True)
            End Set
        End Property

        ''' <summary>
        ''' Gets and Sets the Vertical Scroll position of the control.
        ''' </summary>
        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
    End Class
End Namespace

This will add two properties to a standard RichTextBox: HScrollPos and VScrollPos. These properties will allow you go get and set the horizontal and vertical scrollbar position in your control.

Step 2: Create a test form and try out your control.

Create a Winform in the same project as your custom control. Drop the custom control in the test form and add a button on the form. In the form's Click event, add the following code to view your custom control's vertical scroll position.

Console.WriteLine(myRichTextBox1.VScrollPos)

A few things to watch for:

  1. If your control is not currently
    displaying a vertical scrollbar, the
    call to HScrollPos will crash your
    program. There are a few obviously
    ways around this (check that the
    scrollbar is being displayed before
    checking the property, or making the
    vertical scrollbar always visible, etc.).

  2. Depending on how the control (and
    possibly form) is sized (not to
    mention changes in the text contents
    of the control), setting the
    VScrollPos can crash your program or
    not restore it to the position it
    was in when the VScrollPos value was
    saved.

  3. I've never used this code. I
    thought your question was
    interesting and did a little bit of
    research to come up with my answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文