37.1.4 Mouse Messages

The mouse generates an input event whenever the user moves the mouse or presses or releases a mouse button. Windows converts mouse input events into messages and posts them to the appropriate thread message queue. When mouse messages are posted faster than a thread can process them, Windows discards all but the most recent mouse message.

A window receives a mouse message when a mouse event occurs while the mouse cursor is within the borders of the window or when the window has captured the mouse. Mouse messages are divided into two groups: client-area mouse messages and nonclient-area mouse messages. Typically, an application processes client-area mouse messages and ignores nonclient-area messages.

37.1.4.1 Client-Area Mouse Messages

A window receives a client-area mouse message when a mouse event occurs within the window's client area. The system posts the WM_MOUSEMOVE message to the window when the user moves the mouse cursor within the client area. It posts one of the following messages when the user presses or releases a mouse button while the cursor is within the client area:

Message Meaning

WM_LBUTTONDBLCLK The left mouse button was double-clicked.
WM_LBUTTONDOWN The left mouse button was pressed.
WM_LBUTTONUP The left mouse button was released.
WM_MBUTTONDBLCLK The middle mouse button was double-clicked.
WM_MBUTTONDOWN The middle mouse button was pressed.
WM_MBUTTONUP The middle mouse button was released.
WM_RBUTTONDBLCLK The right mouse button was double-clicked.
WM_RBUTTONDOWN The right mouse button was pressed.
WM_RBUTTONUP The right mouse button was released.

Message Parameters

The lParam parameter of a client-area mouse message indicates the position of the mouse-cursor hot spot. The low-order word indicates the x-coordinate of the hot spot, and the high-order word indicates the y-coordinate. The coordinates are given in client coordinates. In the client coordinate system, all points on the screen are given relative to the upper-left corner of the client area, which has x- and y- coordinates of 0.

The wParam parameter contains flags that indicate the status of the other mouse buttons and the CTRL and SHIFT keys at the time of the mouse event. You can check for these flags when mouse-message processing depends on the state of another mouse button or of the CTRL or SHIFT key. The lParam parameter can be a combination of the following flags:

Value Meaning

MK_CONTROL The CTRL key is down.
MK_LBUTTON The left mouse button is down.
MK_MBUTTON The middle mouse button is down.
MK_RBUTTON The right mouse button is down.
MK_SHIFT The SHIFT key is down.

Double-Click Messages

The system generates a double-click message when the user clicks a mouse button twice in quick succession. When the user clicks a button, the system establishes a rectangle centered around the mouse-cursor hot spot. It also marks the time at which the click occurred. When the user clicks the same button a second time, the system determines whether the hot spot is still within the rectangle and calculates the elapsed time since the first click. If the hot spot is still within the rectangle and the elapsed time hasn't exceeded the double-click timeout value, the system generates a double-click message.

You can use the GetDoubleClickTime function to retrieve the double-click timeout value, and the SetDoubleClickTime function to set the value. Alternatively, you can set the double-click timeout value by using the SPI_SETDOUBLECLICKTIME flag with the SystemParametersInfo function. You can set the size of the rectangle that Windows uses to detect double-clicks by passing the SPI_SETDOUBLECLICKWIDTH and SPI_SETDOUBLECLICKHEIGHT flags to the SystemParametersInfo function. Note, however, that setting the double-click timeout value and rectangle affects all applications.

An application-defined window does not, by default, receive double-click messages. Because of the system overhead involved in generating double-click messages, these messages are generated only for windows belonging to classes that have the CS_DBLCLKS class style. Your application must set this style when registering the window class. For more information about window classes, see Chapter 34, “Window Classes.”

A double-click message is always the third message in a four-message series. The first two messages are the button down and button up messages generated by the first click. The second click generates the double-click message followed by another button up message. For example, double-clicking the left mouse button generates the following message sequence:

WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLK
WM_LBUTTONUP

Because a window always receives a button down message before receiving a double-click message, an application typically uses a double-click message to extend a task that it began during a button down message. For example, when the user clicks a color in the color palette of Windows PaintBrush, PaintBrush displays the selected color next to the color palette. When the user double-clicks a color, PaintBrush displays the color and also invokes the Edit Colors dialog box.

37.1.4.2 Nonclient-Area Mouse Messages

A window receives a nonclient-area mouse message when a mouse event occurs in any part of a window except the client area. A window's nonclient area consists of its border, menu bar, title bar, scroll bar, system menu, minimize button, and maximize button.

Windows generates nonclient-area messages primarily for its own use. For example, Windows uses nonclient-area messages to change the mouse cursor to a two-headed arrow when the mouse-cursor hot spot moves into a window's border. A window must pass nonclient mouse messages to the DefWindowProc function to take advantage of Windows' built-in mouse interface.

There is a corresponding nonclient-area mouse message for each client-area mouse message. The names of the client-area and nonclient-area mouse messages are similar except that the named constants for the nonclient-area messages include the letters “NC”. For example, moving the mouse cursor in the nonclient area generates a WM_NCMOUSEMOVE message, and pressing the left mouse button while the cursor is in the nonclient area generates a WM_NCLBUTTONDOWN message.

The lParam parameter of a nonclient-area mouse message is a POINTS structure that contains the x- and y-coordinates of the mouse-cursor hot spot. Unlike client-area mouse messages, the coordinates are given in screen coordinates rather than client coordinates. In the screen coordinate system, all points on the screen are relative to the upper-left corner of the screen, which has x- and y- coordinates of 0.

The wParam parameter contains a hit-test code, which is a value that indicates where in the nonclient area the mouse event occurred. The following section explains the purpose hit-test codes.