User input can also come from the mouse. Windows sends mouse messages to the application when the user moves the cursor into and through a window or presses or releases a mouse button while the cursor is in the window. Windows generates mouse messages in response to the following events:
Message | Event |
WM_MOUSEMOVE | User moves the cursor into or through the window | |
WM_LBUTTONDOWN | User presses the left button | |
WM_LBUTTONUP | User releases the left button | |
WM_LBUTTONDBLCLK | User presses, releases, and presses again the left button within the system's defined double-click time | |
WM_MBUTTONDOWN | User presses the middle button | |
WM_MBUTTONUP | User releases the middle button | |
WM_MBUTTONDBLCLK | User presses, releases, and presses again the middle button within the system's defined double-click time | |
WM_RBUTTONDOWN | User presses the right button | |
, | ||
WM_RBUTTONUP | User releases the right button | |
WM_RBUTTONDBLCLK | User presses, releases, and presses again the right button within the system's defined double-click time |
The wParam parameter of each button includes a bitmask specifying the current state of the keyboard and mouse buttons, such as whether the mouse buttons, SHIFT key, and CTRL key are down. The lParam parameter contains the the x- and y-coordinates of the cursor.
Windows sends mouse messages to a window only if the cursor is in the window or if you have captured mouse input by using the SetCapture function. The SetCapture function directs Windows to send all mouse input, regardless of where the cursor is, to the specified window. Applications typically use this function to take control of the mouse when carrying out some critical operation with the mouse, such as selecting something in the client area. Capturing the mouse prevents other applications from taking control of the mouse before the operation is completed.
Since the mouse is a shared resource, it is important to release the captured mouse as soon as you have finished the operation. You release the mouse by using the ReleaseCapture function. Use the GetCapture function to determine which window, if any, has the captured mouse.
Windows sends double-click messages to a window function only if the corresponding window class has the CS_DBLCLKS style. You must set this style while registering the window class. A double-click message is always the third message in a four-message series. The first two messages are the first button press and release. The second button press is replaced with the double-click message. The last message is the second release. Remember that a double-click message occurs only if the first and second press occur within the system's defined double-click time. You can retrieve the current double-click time by using the GetDoubleClickTime function. You can set it by using the SetDoubleClickTime function, but be aware that this sets the double-click time for all applications, not just your own.