Scroll bars are typically created as special child windows in the parent frame window. They react to user mouse and keyboard events by sending notification messages to the parent window. This is similar to the mechanism used for other control windows, which is described in the preceding section, “Notification Messages from Child Windows,” on page 316.
One difference between scroll bars and other types of control windows is that scroll bars do not use WM_COMMAND messages for notification. Instead, scroll bars have their own messages, WM_VSCROLL and WM_HSCROLL, which are used to notify the parent window of user interaction with the scroll bar.
·To use the Foundation's message maps to handle scrolling:
Define functions to handle messages from the vertical and/or horizontal scroll bars and define entries in the frame window's message map to point to these functions. The functions must be named OnVScroll and OnHScroll.
The macros that define the message-map entries are named ON_WM_VSCROLL and ON_WM_HSCROLL. Consider the following class declaration and message map:
class CMyWnd : public CFrameWnd
{
public:
afx_msg void OnVScroll( UINT nSBCode, UINT nPos, CWnd* pScrollBar );
afx_msg void OnHScroll( UINT nSBCode, UINT nPos, CWnd* pScrollBar );
// other class declaration stuff ...
DECLARE_MESSAGE_MAP();
};
BEGIN_MESSAGE_MAP( CMyWnd, CFrameWnd )
ON_WM_VSCROLL()
ON_WM_HSCROLL()
END_MESSAGE_MAP()
Notice that the functions OnVScroll and OnHScroll have three arguments.
The first UINT argument contains a code that specifies which scrolling action has been requested, such as SB_LINEDOWN, SB_PAGEUP, or SB_THUMBPOSITION. The possible values are the same as those for the wParam argument to a WM_VSCROLL or WM_HSCROLL message.
The second UINT argument contains an absolute position indicator that applies only to SB_THUMBPOSITION and SB_THUMBTRACK messages.
The final argument is a CWnd pointer that points to the scroll-bar object that sent the message. If the frame window was created with the WS_HSCROLL or WS_VSCROLL, then the scroll bars are not real child windows and this argument will be NULL. If it is not NULL, you can cast this pointer to a CScrollBar pointer and call scroll-bar member functions to get or set information about the scroll bar.