To carry out a selection, use the statements described in Section 6.3, “Using the Cursor with the Mouse.” Add the following statements to your window procedure:
case WM_LBUTTONDOWN:
fTrack = TRUE;
szStr[0] = '\0';
PrevX = LOWORD(lParam);
PrevY = HIWORD(lParam);
if (!(wParam & MK_SHIFT)) { /* If shift key */
OrgX = LOWORD(lParam); /* is not pressed */
OrgY = HIWORD(lParam);
}
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
/*
* Capture all input even if the mouse goes outside
* the window.
*/
SetCapture(hwnd);
break;
case WM_MOUSEMOVE:
{
RECT rectClient;
int NextX;
int NextY;
if (fTrack) {
NextX = LOWORD(lParam);
NextY = HIWORD(lParam);
/* Do not draw outside the window's client area. */
GetClientRect(hwnd, &rectClient);
if (NextX < rectClient.left)
NextX = rectClient.left;
else
if (NextX >= rectClient.right)
NextX = rectClient.right - 1;
if (NextY < rectClient.top)
NextY = rectClient.top;
else
if (NextY >= rectClient.bottom)
NextY = rectClient.bottom - 1;
/*
* If the mouse position has changed, then clear the
* previous rectangle and draw the new one.
*/
if (NextX != PrevX || NextY != PrevY) {
hdc = GetDC(hwnd);
SetROP2(hdc, R2_NOT); /* erases previous box */
MoveTo(hdc, OrgX, OrgY);
LineTo(hdc, OrgX, PrevY);
LineTo(hdc, PrevX, PrevY);
LineTo(hdc, PrevX, OrgY);
LineTo(hdc, OrgX, OrgY);
/* Get the current mouse position. */
PrevX = NextX;
PrevY = NextY;
MoveTo(hdc, OrgX, OrgY); /* draws new box */
LineTo(hdc, OrgX, PrevY);
LineTo(hdc, PrevX, PrevY);
LineTo(hdc, PrevX, OrgY);
LineTo(hdc, OrgX, OrgY);
ReleaseDC(hwnd, hdc);
}
}
}
break;
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;