Discussion: Scrolling Message Handlers

This section explains the message-handlers for vertical and horizontal scrolling.

OnHScroll, OnVScroll, and OnKeyDown

Phone Book provides scrolling support through both the mouse and the keyboard.

When the user clicks the mouse in the vertical scroll bar of the Phone Book window, Windows sends the window a WM_VSCROLL message, which invokes the main window object's OnVScroll member function. Likewise, a click in the horizontal scroll bar invokes OnHScroll for a WM_HSCROLL message.

These functions greatly resemble the code normally seen in the WndProc function that you write for a traditional Windows program that handles scrolling for a window. Each function uses a C switch statement to select an scrolling adjustment based on what part of the scroll bar was clicked. Each case in the switch statement adjusts a scroll increment value appropriately. This value is used to recalculate the “scroll position” and to scroll the window's contents accordingly. Scrolling also forces an update to repaint the window.

In Phone Book, the scroll bars respond not only to the mouse but also to the RIGHT ARROW, LEFT ARROW, PAGE UP, PAGE DOWN, HOME, and END keys. The arrows scroll horizontally. PAGE UP and PAGE DOWN scroll the height of the window. HOME scrolls to the top of the database list. END scrolls to the bottom of the database list. The OnKeyDown member function translates these keys into scrolling actions. These scrolling actions do not change the current selection in the window.