Scroll Bars

Scroll bars are predefined controls that can be positioned anywhere in a window. They allow a user to select a value from a continuous range of values. The scroll bar sends a notification message to its parent window whenever the user clicks the control with the mouse or moves the scroll-bar thumb using the keyboard; this allows the parent window to process the messages so that it can determine the value selected by the user and position the thumb appropriately.

To create a child-window scroll bar, use the SBS_HORZ or SBS_VERT style. You can create a scroll bar with any desired size. If you want the width (of a vertical scroll bar) or height (of a horizontal scroll bar) to match the size of a window scroll bar, you can use the appropriate system metrics, as shown in the following example:

hScrollBar = CreateWindow("Scrollbar", NULL,

WS_CHILD | WS_VISIBLE | SBS_VERT,

20, 20,

GetSystemMetrics (SM_CXVSCROLL), 50,

hWnd, IDSCROLLBAR, hInst, NULL);

The GetSystemMetrics function returns the current value for SM_CXVSCROLL, which is the width of a standard window scroll bar.

Scroll-bar controls do not have a special set of notification messages. Instead, they send the same messages (WM_HSCROLL and WM_VSCROLL) sent by window scroll bars. The wParam parameter of these messages contains a value that indicates what kind of scrolling is being performed. Your application uses this information to determine how to position the scroll-bar thumb and what that position means to your application. Table 20.1 lists these wParam values and describes the user action which generates them.

Table 20.1 User Interface for Scroll Bar

Message wParam Value Mouse Keyboard

SB_LINEUP User clicked the Up or Left arrow of the scroll bar User pressed LEFT ARROW or UP ARROW
SB_LINEDOWN User clicked the Down or Right arrow of the scroll bar, User pressed RIGHT ARROW or DOWN ARROW  
SB_PAGEUP User clicked above or to the left of the scroll-bar thumb, User pressed PGUP  
SB_PAGEDOWN User clicked below or to the right of the scroll-bar thumb User pressed PGDN
SB_ENDSCROLL User clicked anywhere on the scroll bar except the thumb, None  
SB_THUMBTRACK User is dragging the thumb None
SB_THUMBPOSITION User stopped dragging the thumb None
SB_TOP None User pressed HOME
SB_BOTTOM None User pressed END

Windows is capable of properly positioning the thumb of a scroll bar associated with a list box or an edit control based on the contents of the control. However, a scroll bar that is a child-window control represents a range of values known only to your application. As a result, it is the responsibility of your application to set the scrolling range for the scroll bar and to position the thumb each time the user moves it.

The SetScrollRange function establishes the range of values that the scroll bar represents. For example, if your application has a scroll bar with which the user can select a day in a given month, you would call SetScrollRange to set the scroll range to the number of days in a particular month. The following shows how your application could set the range from the month of January:

SetScrollRange(hScrollBar, SB_CTL, 1, 31, 1)

In this example, SB_CTL informs Windows that the scroll bar is a separate scroll-bar control, not a scroll bar associated with a window. The third and fourth parameters specify the scroll-bar range, and the fourth parameter is set to 1 to direct windows to redraw the scroll bar to reflect the new range.

Even though you have established the range of values that the scroll bar represents, Windows still cannot properly position the thumb of the scroll bar when the user moves it; that remains the responsibility of your application. Each time your application receives a WM_HSCROLL or WM_VSCROLL message for the scroll bar, you must check the wParam parameter of the message to determine how far the user moved the thumb. You then call the SetScrollPos function to position the thumb. Also, if your application allows the user to change the value represented by the thumb position without using the scroll bar (such as by typing in an edit control), your application must reposition the thumb based on the new value.