Scroll Bar Messages

Windows sends the window procedure WM_VSCROLL and WM_HSCROLL messages when the scroll bar is clicked with the mouse or the thumb is dragged. Each mouse action on the scroll bar generates at least two messages, one when the mouse button is pressed and another when it is released.

The value of wParam that accompanies the WM_VSCROLL and WM_HSCROLL messages describes what the mouse is doing to the scroll bar. These values of wParam have WINDOWS.H identifiers that begin with SB, which stands for ”scroll bar.“ Although some of these identifiers use the words ”UP“ and ”DOWN,“ they apply to horizontal as well as vertical scroll bars, as you see in Figure 2-9. Your window procedure can receive multiple SB_LINEUP, SB_PAGEUP, SB_LINEDOWN, or SB_PAGEDOWN messages if the mouse button is held down while positioned on the scroll bar. The SB_ENDSCROLL message signals that the mouse button has been released. You can generally ignore SB_ENDSCROLL messages.

When wParam is SB_THUMBTRACK or SB_THUMBPOSITION, the low word of lParam is the current position of the dragged scroll bar. This position is within the minimum and maximum values of the scroll bar range. For other values of wParam, the low word of lParam should be ignored. You can also ignore the high word of lParam.

The Windows documentation indicates that the wParam value can also be SB_TOP and SB_BOTTOM, indicating that the scroll bar has been moved to its minimum or maximum position. However, you will never receive these values for a scroll bar created as part of your application window.

Handling the SB_THUMBTRACK and SB_THUMBPOSITION messages is problematic. If you set a large scroll bar range and the user quickly drags the thumb inside the scroll bar, Windows sends your window function a barrage of SB_THUMBTRACK messages.

Your program may have problems keeping up with these messages. For this reason, most Windows applications ignore these messages and take action only on receipt of SB_THUMBPOSITION, which means that the thumb is again at rest.

However, if you can update your display quickly, you may want to include SB_THUMBTRACK processing in your program. But be aware that users who discover that your program scrolls as they move the scroll bar thumb will undoubtedly try to move it as quickly as possible to see if your program can keep up. They will get an inordinate amount of satisfaction if it cannot.