我有一张SplitContainer
SplitContainer的panel1
由RichTextBox
组成。
Panel2
AutoScroll设置为true。
我希望同步RichTextBox
和Panel2
的滚动,反之亦然。我怎样才能做到这一点?任何想法?
我已经尝试了this,它适用于两个RichTextBox
但不是我的情况。
答案 0 :(得分:1)
获取两个控件的滚动信息:
首先,您需要将以下win32 API( Imports 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
你的下一步是监视哪个滚动条改变....通过计时器等...然后在需要时更新另一个控件。