将 RichTextBox 垂直滚动与 SplitContainer 面板滚动同步

发布于 2024-10-15 19:53:02 字数 433 浏览 4 评论 0原文

我有一个带有 SplitContainer 的 win 表单

SplitContainer 的 panel1RichTextBox 组成。

Panel2 AutoScroll 设置为 true。

我想同步 RichTextBoxPanel2 的滚动,反之亦然。我怎样才能做到这一点?有什么想法吗?

我已经尝试过这个并且它适用于两个RichTextBox 是但不是我的情况。

I have a win form with a SplitContainer

The SplitContainer's panel1 consists of a RichTextBox.

Panel2 AutoScroll is set to true.

I want to synchronize the scrolling of RichTextBox and Panel2 vice versa. How can I do that? any idea?

I've tried this and it is working for two RichTextBoxes but not in my case.

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

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

发布评论

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

评论(1

风渺 2024-10-22 19:53:02

获取两个控件的滚动信息:

首先,您需要将以下 win32 API(导入 System.Runtime.InteropServices)添加到您的项目中,

<DllImport("user32.dll")> _
Private Shared Function GetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, ByRef lpsi As SCROLLINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll")> _
Private Shared Function SetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, <[In]()> ByRef lpsi As SCROLLINFO, ByVal fRedraw As Boolean) As Integer
End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function

'added by edit
Private Const SB_THUMBTRACK As Integer = 5
Private Const WM_VSCROLL As Integer = &H115
Private Const WM_HSCROLL As Integer = &H114

Public Declare Function SetScrollPos Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal nBar As Integer, _
    ByVal nPos As Integer, _
    ByVal bRedraw As Boolean) As Integer

    Private Structure SCROLLINFO
        Public cbSize As UInteger
        Public fMask As UInteger
        Public nMin As Integer
        Public nMax As Integer
        Public nPage As UInteger
        Public nPos As Integer
        Public nTrackPos As Integer
    End Structure

    Private Enum ScrollBarDirection
        SB_HORZ = 0
        SB_VERT = 1
        SB_CTL = 2
        SB_BOTH = 3
    End Enum

    Private Enum ScrollInfoMask
        SIF_RANGE = &H1
        SIF_PAGE = &H2
        SIF_POS = &H4
        SIF_DISABLENOSCROLL = &H8
        SIF_TRACKPOS = &H10
        SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
    End Enum

 'create some public properties to get and set scroll position for any scrollable control:

 Public Property _VerticalScroll(ByVal hwnd As IntPtr) As Integer
     Get
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         Return si.nPos
     End Get
     Set(ByVal value As Integer)
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         If value > si.nMax Then
             value = si.nMax
         End If

         Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
         SetScrollPos(hwnd, Orientation.Vertical, value, True)
         PostMessage(hwnd, WM_VSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

Public Property _HorizontalScroll(ByVal hwnd As IntPtr) As Integer
    Get
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        Return si.nPos
    End Get
    Set(ByVal value As Integer)
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        If value > si.nMax Then
            value = si.nMax
        End If

        Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
        SetScrollPos(hwnd, Orientation.Horizontal, value, True)
        PostMessage(hwnd, WM_HSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

下一步是通过计时器监视哪个滚动条发生变化...等等...然后在需要时更新其他控件。

Get the scroll info for both controls:

First you will need the following win32 API's (Imports System.Runtime.InteropServices) into your project

<DllImport("user32.dll")> _
Private Shared Function GetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, ByRef lpsi As SCROLLINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll")> _
Private Shared Function SetScrollInfo(ByVal hwnd As IntPtr, ByVal fnBar As Integer, <[In]()> ByRef lpsi As SCROLLINFO, ByVal fRedraw As Boolean) As Integer
End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function

'added by edit
Private Const SB_THUMBTRACK As Integer = 5
Private Const WM_VSCROLL As Integer = &H115
Private Const WM_HSCROLL As Integer = &H114

Public Declare Function SetScrollPos Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal nBar As Integer, _
    ByVal nPos As Integer, _
    ByVal bRedraw As Boolean) As Integer

    Private Structure SCROLLINFO
        Public cbSize As UInteger
        Public fMask As UInteger
        Public nMin As Integer
        Public nMax As Integer
        Public nPage As UInteger
        Public nPos As Integer
        Public nTrackPos As Integer
    End Structure

    Private Enum ScrollBarDirection
        SB_HORZ = 0
        SB_VERT = 1
        SB_CTL = 2
        SB_BOTH = 3
    End Enum

    Private Enum ScrollInfoMask
        SIF_RANGE = &H1
        SIF_PAGE = &H2
        SIF_POS = &H4
        SIF_DISABLENOSCROLL = &H8
        SIF_TRACKPOS = &H10
        SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
    End Enum

 'create some public properties to get and set scroll position for any scrollable control:

 Public Property _VerticalScroll(ByVal hwnd As IntPtr) As Integer
     Get
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         Return si.nPos
     End Get
     Set(ByVal value As Integer)
         Dim si As New SCROLLINFO()
         si.cbSize = CUInt(Marshal.SizeOf(si))
         si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
         GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_VERT), si)
         If value > si.nMax Then
             value = si.nMax
         End If

         Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
         SetScrollPos(hwnd, Orientation.Vertical, value, True)
         PostMessage(hwnd, WM_VSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

Public Property _HorizontalScroll(ByVal hwnd As IntPtr) As Integer
    Get
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        Return si.nPos
    End Get
    Set(ByVal value As Integer)
        Dim si As New SCROLLINFO()
        si.cbSize = CUInt(Marshal.SizeOf(si))
        si.fMask = CUInt(ScrollInfoMask.SIF_ALL)
        GetScrollInfo(hwnd, CInt(ScrollBarDirection.SB_HORZ), si)
        If value > si.nMax Then
            value = si.nMax
        End If

        Dim ptrWparam = New IntPtr(SB_THUMBTRACK + &H10000 * value)
        SetScrollPos(hwnd, Orientation.Horizontal, value, True)
        PostMessage(hwnd, WM_HSCROLL, ptrWparam, IntPtr.Zero)
    End Set
End Property

your next move is to monitor which scroll bar changes....via timer etc... then update the other control when needed.

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