Scroll Bar Range and Position

Scroll bars have a ”range“ and a current ”position.“ The range is defined by minimum and maximum integer values. When the thumb is at the top (or left) of the scroll bar, the position of the thumb is the minimum value of the range. At the bottom (or right) of the scroll bar, the thumb position is the maximum value of the range.

The position of the thumb is always a discrete integral value. For instance, a scroll bar with a range from 0 through 4 has five thumb positions, as shown in Figure 2-8. By default, the range of a scroll bar is 0 (top or left) through 100 (bottom or right), but it's easy to change the range to something that is more convenient for the program:

SetScrollRange (hwnd, nBar, nMin, nMax, bRedraw) ;

The nBar parameter is either SB_VERT or SB_HORZ, nMin and nMax are the minimum and maximum positions of the range, and bRedraw is set to TRUE if you want Windows to redraw the scroll bar based on the new range.

You can use SetScrollPos to set a new thumb position within the range:

SetScrollPos (hwnd, nBar, nPos, nRedraw) ;

The nPos parameter is the new position and must be within the range of nMin through nMax. Windows provides similar functions (GetScrollRange and GetScrollPos) to obtain the current range and position of a scroll bar.

When you use scroll bars within your program, you share responsibility with Windows for maintaining the scroll bars and updating the position of the scroll bar thumb. These are Windows' responsibilities for scroll bars:

Handle all scroll bar mouse logic

Provide a ”reverse video“ flash when the user clicks on the scroll bar

Display a ”ghost“ box when the user drags the thumb within the scroll bar

Send scroll bar messages to the window procedure for the window containing the scroll bar

These are your program's responsibilities:

Initialize the range of the scroll bar

Process the scroll bar messages

Update the position of the scroll bar thumb