Scrollbars differ from other controls by passing information to the parent windows by use of the WM_VSCROLL and WM_HSCROLL messages.
When processing these scroll bar messages, the value of the LOWORD (wparam) indicates what action the scrollbar has taken while the lParam parameter enables the application to identify the type of scroll bar. A value of zero indicates a windows scrollbar and any other value holds the handle of the scroll bar control.
The most common scrollbar messages are
SB_LINEUP - is sent when the scrollbar moves up one position
SB_LINEDOWN - is sent when the scrollbar moves down one position
SB_PAGEUP - is sent when the scrollbar is moved up one page
SB_PAGEDOWN - is sent when the scrollbar is moved down one page
SB_LINELEFT - is sent when the scrollbar is moved left one position
SB_LINERIGHT - is sent when the scrollbar is moved right one position
SB_PAGELEFT - is sent when the scrollbar is moved one page left
SB_PAGERIGHT - is sent when the scrollbar is moved one page right
SB_THUMBPOSITION - is sent after the thumbbar is dragged to a new position
SB_THUMBTRACK - is sent while the thumbbar is dragged to a new position
The following API functions can be used to get and set additional scrollbar information
GetScrollInfo() - retrieves the parameters of a scroll bar, including the minimum and maximum scrolling positions, the page size, and the position of the scroll box (thumb). The prototype of this function is
BOOL GetScrollInfo(HWND hwnd,int nBar,LPSCROLLINFO lpsi);
If the function does not retrieve any values, the return value is zero else the return value if non zero.
SetScrollInfo() - the SetScrollInfo function sets the parameters of a scroll bar, including the minimum and maximum scrolling positions, the page size, and the position of the scroll box (thumb). The function also redraws the scroll bar, if requested. The prototype of this function is
int SetScrollInfo(HWND hwnd,int nBar,LPCSCROLLINFO lpsi,BOOL redraw);
where
hwnd - handle to a scroll bar control or a window with a standard scroll bar.
nBar - Specifies the type of scroll bar for which to retrieve parameters. This parameter can be one of the following values
SB_CTL - Retrieves the parameters for a scroll bar control.
SB_HORZ - Retrieves the parameters for the window's standard horizontal scroll bar.
SB_VERT - Retrieves the parameters for the window's standard vertical scroll bar.
lpsi - Pointer to a SCROLLINFO structure(see below).
redraw - Specifies whether the scroll bar is redrawn to reflect any changes. Only relevant when setting scrollbar values
The return value is the current position of the scroll box.
typedef struct tagSCROLLINFO {UINT cbSize;UINT fMask;int nMin; int nMax; UINT nPage; int nPos; int nTrackPos;} SCROLLINFO, *LPSCROLLINFO;
cbSize - specifies the size of the SCROLLINFO structure.
fMask - specifies the scroll bar parameters to set or retrieve. This can be a combination of the following values:
SIF_ALL - Combination of SIF_PAGE, SIF_POS, SIF_RANGE, and SIF_TRACKPOS. SIF_DISABLENOSCROLL - This value is used only when setting a scroll bar's parameters. If the scroll bar's new parameters make the scroll bar unnecessary, disable the scroll bar instead of removing it. SIF_PAGE - The nPage member contains the page size for a proportional scroll bar. SIF_POS - The nPos member contains the thumb bar position, which is not updated while the user drags the thumb bar. SIF_RANGE - The nMin and nMax members contain the minimum and maximum values for the scrolling range. SIF_TRACKPOS - The nTrackPos member contains the current position of the thumb bar while the user is dragging it. |
nMin - minimum scrolling position.
nMax - maximum scrolling position.
nPage - Specifies the page size, in device units. This value to determine the size of the proportional thumb bar.
nPos - Specifies the position of the thumb bar.
nTrackPos - Specifies the current thumb bar position while it is being dragged by the user.
The following short program demonstrates the main window vertical scrollbar by displaying the scroll value within the main window.
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = {0};
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = TEXT("myWindowClass");
RegisterClassEx(&wc);
//create window with scrollbar
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("Scrollbar demo"),WS_THICKFRAME|WS_OVERLAPPEDWINDOW| WS_VISIBLE | WS_OVERLAPPEDWINDOW |WS_VSCROLL,CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT Ps;
SCROLLINFO si;
HDC hdc;
static int yClient;
int totalwords=100;
static int yPos;
static int rows;
switch(msg)
{
case WM_PAINT://traps paint message
{
hdc = BeginPaint(hwnd, &Ps);
TCHAR sbv[10]=TEXT("\0");
int count;
int linecount=0;
int startline=GetScrollPos(hwnd,SB_VERT);
int endline=startline+rows;
int strlength=0;
//output line number
for (count=startline; count <endline; count++)
{
strlength=wsprintf(sbv,TEXT("%d"), count+1);
TextOut(hdc,0,linecount,sbv,strlength);
linecount=linecount+15;
}
EndPaint(hwnd, &Ps);
DeleteDC(hdc);
}
break;
//deals with changes in widows sizr
case WM_SIZE:
yClient = HIWORD (lParam); // Retrieves the height of the client area.
rows=yClient/15;//calculate totoal number of text row in client area
SetScrollRange(hwnd, SB_VERT, 0, totalwords-rows, TRUE);//set scrollbar size
InvalidateRect(hwnd,NULL,TRUE) ;//repaint window
break;
case WM_VSCROLL:
{
//Retrieves attributes of scrollbar
si.cbSize = sizeof (si);
si.fMask = SIF_ALL;
GetScrollInfo (hwnd, SB_VERT, &si);
//respond to scrollbar messages
switch (LOWORD (wParam))
{
// User clicked the HOME keyboard key.
case SB_TOP:
si.nPos = si.nMin;
break;
// User clicked the END keyboard key.
case SB_BOTTOM:
si.nPos = si.nMax;
break;
// User clicked the top arrow.
case SB_LINEUP:
si.nPos -= 1;
break;
// User clicked the bottom arrow.
case SB_LINEDOWN:
si.nPos += 1;
break;
// User dragged the scroll box.
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default:
break;
}
// Set the new scollbar position.
si.fMask = SIF_POS;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
//redraw window contents by manually sending manually sending WM_PAINT
InvalidateRect(hwnd,NULL,TRUE) ;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}