windows_programming_notes.nbk: Home | Index | Next Page: TrackMouseEvent | Previous Page: The dialog manager, part 9: Custom accelerators in dialog boxes


 Tracking the Mouse Cursor

Applications often perform tasks that involve tracking the position of the mouse cursor. Most drawing applications, for example, track the position of 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 cursor, enabling the user to select a word or block of text by clicking and dragging the mouse.

Tracking the cursor typically involves processing the WM_LBUTTONDOWN, WM_MOUSEMOVE, and WM_LBUTTONUP messages. A window determines when to begin tracking the cursor by checking the cursor position provided in the lParam parameter of the WM_LBUTTONDOWN message. For example, a word-processing application would begin tracking the cursor only if the WM_LBUTTONDOWN message occurred while the 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 cursor by processing the stream of WM_MOUSEMOVE messages 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 repeatedly as the mouse moves. A window uses the WM_LBUTTONUP message as a signal to stop tracking the cursor.

In addition, an application can call the TrackMouseEvent function to have the system send other messages that are useful for tracking the cursor. The system posts the WM_MOUSEHOVER message when the cursor hovers over the client area for a certain time period. It posts the WM_MOUSELEAVE message when the cursor leaves the client area. The WM_NCMOUSEHOVER and WM_NCMOUSELEAVE messages are the corresponding messages for the nonclient areas.


windows_programming_notes.nbk: Home | Index | Next Page: TrackMouseEvent | Previous Page: The dialog manager, part 9: Custom accelerators in dialog boxes


Notebook exported on Monday, 7 July 2008, 18:56:50 PM Eastern Daylight Time