Click here to Skip to main content
15,881,044 members
Articles / Programming Languages / Visual Basic
Article

Controlling scrolling with the API

Rate me:
Please Sign up or sign in to vote.
4.74/5 (22 votes)
4 Feb 20042 min read 131.5K   1.5K   27   13
Scrolling with the API.

Introduction

In certain circumstances, we need to be able to control the scroll bars from our code. This may seem like it should be easily done with standard methods when in fact they are buried in the API. I will tell you how I solved this problem in the application I am working on.

Imports

You have to import the System.Runtime.InteropServices.Marshal namespace so if you get an error you can access GetLastWin32Error, a method in this namespace. So, let's begin with the import directive.

VB.NET
Imports System.Runtime.InteropServices.Marshal

Constants

Along with many API functions, there are a slew of structures, types and constants that are at your disposal. For this, I am only going to use a few constants (we really don't need the rest). However, if you are wondering what the other scrollbar constants are, take a look at WinUser.h (located in the .NET SDK directory tree). Here are the constants I will be using:

VB.NET
' Scrollbar direction
'
Const SBS_HORZ = 0
Const SBS_VERT = 1


' Windows Messages
'
Const WM_VSCROLL = &H115
Const WM_HSCROLL = &H114
Const SB_THUMBPOSITION = 4

Declaring the functions

Now comes the (not so) fun part of declaring the functions. The first function we are going to be using is called GetScrollPos (located in afxwin.h). We send this function a handle and a direction. It sends back an integer of the current scroll position.

VB.NET
Private Declare Function GetScrollPos Lib "user32.dll" ( _
        ByVal hWnd As IntPtr, _
        ByVal nBar As Integer) As Integer

  'Example: position = GetScrollPos(textbox1.handle, SBS_HORZ)

The next function is called SetScrollPos, we send SetScrollPos the handle, direction, value and a flag of whether or not it should redraw the control.

VB.NET
 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

'Example: SetScrollPos(hWnd, SBS_HORZ, position, True

The last function we will be using is PostMessageA. PostMessageA sends instructions to the control about what to do next. Normally, you don't have to worry about this as it is handled for you. We send this function a handle, command, parameters and the hWnd of the scroll bar (optional).

VB.NET
Private Declare Function PostMessageA Lib "user32.dll" ( _
        ByVal hwnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Boolean

'Example: PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION _
'                         + &H10000 * position, Nothing)

Usage

Suppose you have a control called textbox1 on your form and you want to scroll it to line 100. You can do this easily using SetScrollPos and PostMessageA. Here is an example of how this can be accomplished:

VB.NET
If (SetScrollPos(hWnd, SBS_HORZ, position, True) <> -1) Then
    PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION +_
                               &H10000 * position, Nothing)
Else
    MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
End If

Remember our imports directive, this is where GetLastWin32Error is located. GetLastWin32Error will return a numeric error code if there is a problem. You can look these error codes up in the WinError.h file.

Now, suppose you want to scroll down a few lines but you don't know where exactly the line you need to scroll to. You can use all three of these functions to accomplish this task:

VB.NET
Dim Position = GetScrollPos(hWnd, SBS_VERT) + 20

If (SetScrollPos(hWnd, SBS_VERT, position, True) <> -1) Then
    PostMessageA(hWnd, WM_VSCROLL, SB_THUMBPOSITION + _
                               &H10000 * position, Nothing)
Else
    MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
End If

Well, that's about it for scrolling with API.

I hope you found this article informative. I will include a sample application that I wrote as I was exploring these functions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.



Comments and Discussions

 
GeneralControlling scrolling with the API Pin
Member 1074591715-Mar-15 8:28
Member 1074591715-Mar-15 8:28 
QuestionWebbrowser Pin
hackerspk30-Aug-12 21:12
hackerspk30-Aug-12 21:12 
QuestionScrolling and the ListView Control Pin
Ken Kutcher18-Mar-09 8:19
Ken Kutcher18-Mar-09 8:19 
QuestionChange scrollbar color in a panel (C#, windows appln.) Pin
AngelaM29-Aug-08 3:00
AngelaM29-Aug-08 3:00 
Hi,

m a newbie to dev. side. Frown | :(

Need one help....cud u pls explain me how to change the color of scrollbar in a panel,
using C# and it is a windows application.

Thanks in advance

NewBie Smile | :)

AnswerExcellent Pin
Andy Davies16-Jun-08 0:00
Andy Davies16-Jun-08 0:00 
GeneralDataGrid Pin
TigerNige25-Oct-05 5:25
TigerNige25-Oct-05 5:25 
GeneralRe: DataGrid Pin
axinte200517-Jul-07 22:24
axinte200517-Jul-07 22:24 
Generalcouple of changes I might suggest: Pin
frumbert19-Jul-05 20:49
frumbert19-Jul-05 20:49 
GeneralMSFlexgrid and SetScrollPos Pin
Anonymous14-Apr-05 2:18
Anonymous14-Apr-05 2:18 
GeneralOverflow errors Pin
Member 28788725-Nov-04 13:24
Member 28788725-Nov-04 13:24 
GeneralGetScrollRange Pin
jnghh11-Mar-04 15:52
jnghh11-Mar-04 15:52 
GeneralRe: GetScrollRange Pin
Aaron Eldreth9-May-04 13:08
Aaron Eldreth9-May-04 13:08 
GeneralRe: GetScrollRange Pin
SnowCarve24-Aug-05 8:47
sussSnowCarve24-Aug-05 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.