[RESOLVED] SCROLLINFO - RTB not updating
Hello everyone.
I had a previous thread about Undo/Redo, but couldn't find it. Anyway, to the point:
I use the following code to save and load the states of a RichTextBox control:
Code:
Public Structure FieldState
Sub New(ByVal control As RichTextBox)
Me.SelectionStart = control.SelectionStart
Me.SelectionLength = control.SelectionLength
Me.Data = control.Rtf
Dim s As New SCROLLINFO(SCROLLINFO.Mask.Position)
SCROLLINFO.GetScrollInfo(control.Handle, SCROLLINFO.Bar.Horizontal, s)
Me.ScrollPosX = s.nPos
SCROLLINFO.GetScrollInfo(control.Handle, SCROLLINFO.Bar.Vertical, s)
Me.ScrollPosY = s.nPos
End Sub
Public Sub Apply(ByVal control As RichTextBox)
control.Rtf = Me.Data
control.Select(Me.SelectionStart, Me.SelectionLength)
Dim s As New SCROLLINFO(SCROLLINFO.Mask.Position)
s.nPos = Me.ScrollPosX
SCROLLINFO.SetScrollInfo(control.Handle, SCROLLINFO.Bar.Horizontal, s, True)
s.nPos = Me.ScrollPosY
SCROLLINFO.SetScrollInfo(control.Handle, SCROLLINFO.Bar.Vertical, s, True)
'control.Invalidate()
End Sub
Public SelectionStart As Integer
Public SelectionLength As Integer
Public Data As String
Public ScrollPosX As Integer
Public ScrollPosY As Integer
Private Structure SCROLLINFO
Public Declare Function GetScrollInfo Lib "user32" (ByVal hWnd As IntPtr, ByVal fnBar As Bar, ByRef lpScrollInfo As SCROLLINFO) As Boolean
Public Declare Function SetScrollInfo Lib "user32" (ByVal hWnd As IntPtr, ByVal fnBar As Bar, ByRef lpScrollInfo As SCROLLINFO, ByVal redraw As Boolean) As Integer
Sub New(ByVal mask As Mask)
Me.cbSize = Runtime.InteropServices.Marshal.SizeOf(Me)
Me.fMask = mask
End Sub
Private cbSize As UInteger
Private fMask As Mask
Public nMin As Integer
Public nMax As Integer
Public nPage As UInteger
Public nPos As Integer
Public nTrackPos As Integer
Public Enum Bar As Integer
Horizontal = 0
Vertical = 1
Client = 2
Both = 3
End Enum
Public Enum Mask As UInteger
Range = &H1
Page = &H2
Position = &H4
DisableNoScroll = &H8
TrackPosition = &H10
ALL = Range Or Page Or Position Or TrackPosition
End Enum
End Structure
End Structure
Usage:
Code:
Dim s As New FieldState(RichTextBox1)
s.Apply(RichTextBox1)
This works almost 100% right, but there is one problem: the textbox does not update the scroll states. The scrollbars visibly change, but contents don't. Also, when you click the up/down buttons on the Scrollbar, it jumps to a different position and moves from there. So, how can I make that scroll information go through to the textbox control? :confused:
Re: SCROLLINFO - RTB not updating
try this. it's an overload of sendmessage:
vb Code:
Private Declare Auto Function SendScrollPosMessage _
Lib "user32.dll" Alias "SendMessage" ( _
ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As IntPtr, _
ByRef lParam As Point) As Integer
Private Const WM_USER As Integer = &H400
Private Const EM_SETSCROLLPOS As Integer = WM_USER + 222
vb Code:
SendScrollPosMessage(ctrl.Handle, EM_SETSCROLLPOS, New IntPtr(0), scrollValues)
Re: SCROLLINFO - RTB not updating
Re: SCROLLINFO - RTB not updating
Thanks paul, I cleared my user thread history recently so couldn't find it back. :rolleyes:
I'll test the sendmessage command.
EDIT
Yep that works nicely, I got to look in the RichEdit.h file some more. :D
Code:
Public Structure FieldState
Sub New(ByVal control As RichTextBox)
Me.SelectionStart = control.SelectionStart
Me.SelectionLength = control.SelectionLength
Me.Data = control.Rtf
SendMessageO(control.Handle, EM_GETSCROLLPOS, 0, Me.ScrollPosition)
End Sub
Public Sub Apply(ByVal control As RichTextBox)
control.Rtf = Me.Data
control.Select(Me.SelectionStart, Me.SelectionLength)
SendMessageO(control.Handle, EM_SETSCROLLPOS, 0, Me.ScrollPosition)
End Sub
Public SelectionStart As Integer
Public SelectionLength As Integer
Public Data As String
Public ScrollPosition As Point
End Structure