Finally, when the user releases the left button, save the final point and signal the end of the selection process. The following statements complete the selection:
case WM_LBUTTONUP:
bTrack = FALSE; /* No longer carrying out a selection */
ReleaseCapture(); /* Release hold on mouse input */
X = LOWORD(lParam); /* Save the current value */
Y = HIWORD(lParam);
break;
When the application receives a WM_LBUTTONUP message, it immediately sets bTrack 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 WM_LBUTTONDOWN, records the selection the user has made. The application can now operate on the selection, and can redraw the selection rectangle when necessary.
For some applications, you might want to check the final cursor position to make sure 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.
The ReleaseCapture function is required since a corresponding SetCapture function was called. In general, you should release the mouse immediately after the mouse capture is no longer needed.