Capturing Mouse Input
A version of this page is also available for
4/8/2010
Applications process touch events as a click of the left mouse button, so support for mouse input can be important to your OS design.
The OS usually posts a mouse message to the window that contains the hot spot of the cursor when the mouse event occurs. An application can change this behavior by using the SetCapture function to route mouse messages to a specific window. The specified window receives all of the mouse messages until the application calls the ReleaseCapture function or calls the SetCapture function to specify another capture window, or until the user clicks on a window that another thread created.
When the mouse capture changes, the OS sends a WM_CAPTURECHANGED message to the window that is losing the mouse capture. The lParam parameter of this message specifies a handle to the window that is gaining the mouse capture.
Only the foreground window can capture mouse input. When a background window attempts to capture mouse input, the background window receives messages only for mouse events that occur when the hot spot of the cursor is located within the visible portion of the window.
Capturing mouse input is useful if a window must receive all mouse input, even when the cursor moves outside the window. For example, an application typically tracks the cursor position after the user presses a mouse button down and follows the cursor until the user releases the mouse button. If an application has not captured mouse input and the user releases the mouse button outside the window, the window does not receive the message that indicates that the user released the button.
A thread can use the GetCapture function to determine whether one of the windows that the thread created has captured the mouse. If one of the windows that the thread created has captured the mouse, GetCapture retrieves a handle to the window.