6.3.4 Ending a Graphics Selection

Finally, when the user releases the left button, your application should save the final point and signal the end of the selection process. The following statements complete the selection:

case WM_LBUTTONUP:
    fTrack = FALSE;   /* no longer carrying out selection */
    ReleaseCapture(); /* releases hold on mouse input     */

    X = LOWORD(lParam); /* saves current value            */
    Y = HIWORD(lParam);
    break;

When the application receives a WM_LBUTTONUP message, it immediately sets fTrack to FALSE to indicate that selection processing has been completed. It also releases the mouse capture by using the ReleaseCapture function. It then saves the current mouse position in the variables X and Y. This, together with the selection-origin information saved on receiving the WM_LBUTTONDOWN message, records the selection the user has made. The application can now manipulate the selection and can redraw the selection rectangle, as necessary.

For some of your applications, you might want to check the final cursor position to ensure that it represents a point to the lower right of the original point. This is the way most rectangles are described—by their upper-left and lower-right corners.

Note that the ReleaseCapture function is required, since a corresponding SetCapture function was called. In general, the application should release the mouse immediately after the mouse capture is no longer needed.