Showing the Selection

As the user makes the selection, you need to provide feedback about his or her progress. You can do this by drawing a border around the rectangle by using the LineTo function on each new WM_MOUSEMOVE message. To prevent losing information already on the display, you need to draw a line that inverts the screen rather than drawing over it. You can do this by using the SetROP2 function to set the binary raster mode to R2_NOT. The following statements perform this function:

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;

The application processes the WM_MOUSEMOVE message only if bTrack is TRUE (that is, if a selection is in progress). The purpose of the WM_MOUSEMOVE processing is to remove the border around the previous rectangle and draw a new border around the rectangle described by the current and original positions. Since the border is actually the inverse of what was originally on the display, inverting again restores it completely. The first four LineTo functions remove the previous border. The next four draw a new border. Before drawing the new border, the PrevX and PrevY values are updated by assigning them the current values contained in the lParam parameter.