Skip to content

SysWin Gui ScrollBar

Paul Squires edited this page Oct 31, 2026 · 1 revision

Home › All Categories › sys/win › sys/win/gui › ScrollBar

ScrollBar

Shows, hides, enables and positions a scroll bar control, and reads and sets its range.

These functions are for a scroll bar control, a window of its own that the program places anywhere, not for the scroll bars along the edges of a window with WS_HSCROLL or WS_VSCROLL. Create one with CWindow.AddControl( "HScrollBar", ... ) or "VScrollBar", which also sets a range of 0 to 100 and the position 0. Every function here takes the control's handle first and is written with the namespace: ScrollBar.GetPos( hSB ).

A scroll bar does not move its own thumb. When the user clicks or drags it, the parent receives WM_HSCROLL (or WM_VSCROLL) with the scroll bar's handle in lParam and an SB_* request in the low word of wParam; the program works out the new position and calls SetPos. For a proportional thumb and 32-bit tracking positions use the Windows function SetScrollInfo with SB_CTL.

Importing

#import "sys/win"
#import "sys/win/gui"
using win, gui

Example

A horizontal scroll bar from 0 to 255 that answers the arrows, the channel and the thumb, and puts its value in the caption.

#import "sys/win"
#import "sys/win/gui"
using win, gui

const IDC_SCROLL = 101

declare function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT

function main() as long
    SetProcessDpiAwareness( PROCESS_SYSTEM_DPI_AWARE )
    dim pWindow as CWindow = "ScrollBarDemo"
    dim hWin as HWND = pWindow.Create( NULL, "Value: 0", @WndProc )
    pWindow.SetClientSize( 340, 80 )
    pWindow.Center()
    dim hSB as HWND = pWindow.AddControl( "HScrollBar", hWin, IDC_SCROLL, "", 20, 30, 300, 20 )
    ScrollBar.SetRange( hSB, 0, 255, true )
    return pWindow.DoEvents( SW_NORMAL )
end function

function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
    select case uMsg
        case WM_HSCROLL
            dim hSB as HWND = cast( HWND, lParam )
            if hSB = GetDlgItem( hwnd, IDC_SCROLL ) then
                dim as long nMin, nMax
                ScrollBar.GetRange( hSB, nMin, nMax )
                dim nPos as long = ScrollBar.GetPos( hSB )
                select case LOWORD( wParam )
                    case SB_LINELEFT : nPos -= 1
                    case SB_LINERIGHT : nPos += 1
                    case SB_PAGELEFT : nPos -= 16
                    case SB_PAGERIGHT : nPos += 16
                    case SB_THUMBTRACK, SB_THUMBPOSITION : nPos = HIWORD( wParam )
                    case SB_LEFT : nPos = nMin
                    case SB_RIGHT : nPos = nMax
                end select
                nPos = iif( nPos < nMin, nMin, iif( nPos > nMax, nMax, nPos ) )
                ScrollBar.SetPos( hSB, nPos, true )
                SetWindowText( hwnd, "Value: " & nPos )
            end if
            return 0
        case WM_DESTROY
            PostQuitMessage( 0 )
            return 0
    end select
    return DefWindowProcW( hwnd, uMsg, wParam, lParam )
end function

end main()

Functions

Function Does
GetPos Returns the thumb's position.
SetPos Moves the thumb.
GetRange Reads the minimum and maximum positions.
SetRange Sets the minimum and maximum positions.
Enable Enables or disables the arrows.
Show Shows, or shows or hides, the scroll bar.
Hide Hides the scroll bar.

Reference

GetPos

function GetPos( byval hSB as HWND ) as long

Returns the thumb's position, or 0 on failure.

Example

print "position:"; ScrollBar.GetPos( hCtrl )

Microsoft Learn: GetScrollPos

SetPos

function SetPos( byval hSB as HWND, byval nPos as long, byval fRedraw as boolean ) as boolean

Moves the thumb to nPos, which should lie within the range; fRedraw true redraws the scroll bar at once.

Returns the number SetScrollPos gives back, a position, turned into a boolean: true when that position is nonzero, false when it is 0 or the call failed. It is not a success flag, and which position Windows returns (the old one or the new) depends on the version of the control in use.

Example

ScrollBar.SetPos( hCtrl, 50, true )

Microsoft Learn: SetScrollPos

GetRange

function GetRange overload( byval hSB as HWND, byval posMin as long ptr, byval posMax as long ptr ) as boolean
function GetRange overload( byval hSB as HWND, byref posMin as long, byref posMax as long ) as boolean

Reads the minimum and maximum positions into posMin and posMax.

Returns true on success.

Example

dim as long nMin, nMax
if ScrollBar.GetRange( hCtrl, nMin, nMax ) then print "from"; nMin; " to"; nMax

Microsoft Learn: GetScrollRange

SetRange

function SetRange( byval hSB as HWND, byval posMin as long, byval posMax as long, byval fRedraw as boolean ) as boolean

Sets the minimum and maximum positions; fRedraw true redraws the scroll bar.

Returns true on success.

Example

ScrollBar.SetRange( hCtrl, 0, 1000, true )

Microsoft Learn: SetScrollRange

Enable

function Enable( byval hSB as HWND, byval flags as UINT ) as boolean

Enables or disables one or both arrows: flags is ESB_ENABLE_BOTH, ESB_DISABLE_BOTH, ESB_DISABLE_LEFT (or ESB_DISABLE_UP) or ESB_DISABLE_RIGHT (ESB_DISABLE_DOWN).

Returns true when the arrows changed state; false when they were already in that state or on failure.

Example

ScrollBar.Enable( hCtrl, ESB_DISABLE_BOTH )

Microsoft Learn: EnableScrollBar

Show

function Show overload( byval hSB as HWND, byval fShow as boolean ) as boolean
function Show overload( byval hSB as HWND ) as boolean

Shows the scroll bar, or with fShow false hides it, with ShowWindow.

Returns whether the scroll bar was visible before the call, as ShowWindow does: not whether the call succeeded.

Example

ScrollBar.Show( hCtrl, GetWindowClientWidth( hwnd ) < 400 )

Microsoft Learn: ShowWindow

Hide

function Hide( byval hSB as HWND ) as boolean

Hides the scroll bar.

Returns true when it was visible before the call, false when it was already hidden.

Example

ScrollBar.Hide( hCtrl )

Microsoft Learn: ShowWindow

See also

Clone this wiki locally