Handling Scroll Bar Requests

Unlike other controls, a scroll bar does not send WM_COMMAND messages. Rather, the scroll bar sends WM_HSCROLL and WM_VSCROLL messages to the window procedure when a user taps the scroll bar or drags the scroll box. The low-order words of WM_VSCROLL and WM_HSCROLL each contain a notification message that indicates the direction and magnitude of the scrolling action.

When you process the WM_HSCROLL and WM_VSCROLL messages, you should examine the scroll bar notification message and calculate the scrolling increment. After you apply the increment to the current scrolling position, you can scroll the window to the new position by using the ScrollWindowEx function. You can use the SetScrollInfo function to adjust the position of the scroll box.

The corresponding SBM_SETSCROLLINFO message passes parameters into the SCROLLINFO structure to indicate the new scroll box position. The SBM_GETSCROLLINFO message retrieves the current position contained within the SCROLLINFO structure.

After you scroll a window, it makes part of the window's client area invalid. To ensure that the invalid region is updated, use the UpdateWindow function to generate a WM_PAINT message.

Usually an application scrolls the content of a window in the direction opposite to that indicated by the scroll bar. For example, when a user taps the gray area below the scroll box, an application scrolls the object in the window upward to reveal a portion of the object below the visible portion. An application can also scroll a rectangular region using the ScrollDC function.

When you process the WM_CREATE message you can set scrolling units. It is convenient to base the scrolling units on the dimensions of the font associated with the window display context. To retrieve the font dimensions for a specific display context, use the GetTextMetrics function. When you process the WM_SIZE message, you can adjust the scrolling range and scrolling position to reflect the dimensions of the client area as well as the number of lines of text displayed.