同步RichTextBox垂直滚动和SplitContainer面板滚动

时间:2022-03-12 19:47:24

I have a win form with a SplitContainer

我用SplitContainer赌赢了

The SplitContainer's panel1 consists of a RichTextBox.

SplitContainer的panel1由RichTextBox组成。

Panel2 AutoScroll is set to true.

Panel2自动滚动被设置为true。

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

我想要同步RichTextBox和Panel2的滚动。我怎么做呢?任何想法?

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

我试过这个,它对两个richtextbox有效,但对我来说不行。

1 个解决方案

#1


1  

Get the scroll info for both controls:

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

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

首先,您需要将以下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

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

你的下一步是监控,滚动条变化....通过定时器等等……然后在需要时更新其他控件。

#1


1  

Get the scroll info for both controls:

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

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

首先,您需要将以下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

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

你的下一步是监控,滚动条变化....通过定时器等等……然后在需要时更新其他控件。