I am currently researching “flick” scrolling for Windows XP and over. As part of this I need the ability to scroll controls in code.
To do this I have created a wrapper around the scrolling apis to assist with this.
Here’s how to use it for an autoscrolling panel:
Dim loScrollIt As New ScrollController(Panel1)
loScrollIt.VerticalScroll(20)
Here’s the code: Imports System.Runtime.InteropServices ' Scrollbar direction ' Windows Messages Private Declare Function GetScrollPos Lib "user32.dll" ( _ 'Example: position = GetScrollPos(textbox1.handle, SBS_HORZ) 'Example: SetScrollPos(hWnd, SBS_HORZ, position, True
Private Declare Function PostMessageA Lib "user32.dll" ( _ 'Example: PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION _ Private moControl As Control
Public Sub New(ByVal controlToScroll As Control)
moControl = controlToScroll If (SetScrollPos(liHwnd, SBS_VERT, Position, True) <> -1) Then
PostMessageA(liHwnd, WM_VSCROLL, SB_THUMBPOSITION + _ End Sub
End Class
Public Class ScrollController
'
Const SBS_HORZ = 0
Const SBS_VERT = 1
'
Const WM_VSCROLL = &H115
Const WM_HSCROLL = &H114
Const SB_THUMBPOSITION = 4
ByVal hWnd As IntPtr, _
ByVal nBar As Integer) As Integer
Private Declare Function SetScrollPos Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal nBar As Integer, _
ByVal nPos As Integer, _
ByVal bRedraw As Boolean) As Integer
ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Boolean
' + &H10000 * position, Nothing)
End Sub
Public Sub VerticalScroll(ByVal amount As Integer)
Dim liHwnd As IntPtr = moControl.Handle
Dim Position = GetScrollPos(liHwnd, SBS_VERT) + amount
&H10000 * Position, Nothing)
End If
Pingback: Catching Application Level Mouse Events in Winforms « An Original Idea
Pingback: Flick Scrolling in Winforms « An Original Idea