Add the WM_LBUTTONDOWN, WM_MOUSEMOVE, and WM_LBUTTONUP Cases

For information on carrying out a selection, see the statements described in “Letting the User Select Information with the Mouse”. Add the following statements to your window function:

case WM_LBUTTONDOWN:

bTrack = TRUE;

strcpy(str, “”);

PrevX = LOWORD(lParam);

PrevY = HIWORD(lParam);

if(!(wParam & MK_SHIFT)) { /* If shift key is not pressed */

OrgX = LOWORD(lParam);

OrgY = HIWORD(lParam);

}

InvalidateRect(hWnd, NULL, TRUE);

UpdateWindow(hWnd);

/* Capture all input even if the mouse goes outside of window */

SetCapture(hWnd);

break;

case WM_MOUSEMOVE:

{

RECT rectClient;

int NextX;

int NextY;

if(bTrack) {

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 the 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 the 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:

bTrack = FALSE; /* Ignores mouse input */

ReleaseCapture(); /* Releases hold on mouse input */

X = LOWORD(lParam); /* Saves the current value */

Y = HIWORD(lParam);

break;