CAPTURING THE MOUSE

A window procedure normally receives mouse messages only when the mouse cursor is positioned over the client or nonclient area of the window. A program that needs to receive mouse messages when the mouse is outside the window can ”capture“ the mouse.

Capturing the mouse is easier than baiting a mousetrap. You need only call:

SetCapture (hwnd) ;

After this function call, Windows sends all mouse messages to the window procedure for the window whose handle is hwnd. The mouse messages are always client-area messages, even when the mouse is in a nonclient area of the window. The lParam parameter still indicates the position of the mouse in client-area coordinates. These x- and y-coordinates, however, can be negative if the mouse is to the left of or above the client area.

During the time the mouse is captured, system keyboard functions are also disabled. When you want to release the mouse, call:

ReleaseCapture () ;

This returns processing to normal.