37.2.1 Tracking the Mouse Cursor

Windows applications often perform tasks that involve tracking the position of the mouse cursor. Most drawing applications, for example, track the mouse cursor during drawing operations, allowing the user to draw in a window's client area by dragging the mouse. Word processing applications also track the mouse cursor, allowing the user to select a word or block of text by clicking and dragging the mouse.

Tracking the mouse cursor typically involves processing the WM_LBUTTONDOWN, WM_MOUSEMOVE, and WM_LBUTTONUP messages. A window determines when to begin tracking the mouse cursor by checking the mouse-cursor position provided in the lParam parameter of the WM_LBUTTONDOWN message. For example, a word processing application would begin tracking the mouse cursor only if the WM_LBUTTONDOWN message occurred while the mouse cursor was on a line of text, but not if it was past the end of the document.

A window tracks the position of the mouse cursor by processing the stream of WM_MOUSMOVE messages that are posted to the window as the mouse moves. Processing the WM_MOUSEMOVE message typically involves a repetitive painting or drawing operation in the client area. For example, a drawing application might redraw a line repeated as the mouse moves. A window uses the WM_BUTTONUP message as a signal to stop tracking the mouse cursor.