Drawing with the Mouse

You can permit the user to draw lines with the mouse by having your window procedure draw while processing the WM_MOUSEMOVE message. Windows sends the WM_MOUSEMOVE message to the window procedure whenever the user moves the cursor within the window. To draw lines, the window procedure can retrieve a display DC and draw a line in the window between the current and previous cursor positions.

In the following example, the window procedure prepares for drawing when the user presses and holds the left mouse button (sending the WM_LBUTTONDOWN message). As the user moves the cursor within the window, the window procedure receives a series of WM_MOUSEMOVE messages. For each message, the window procedure draws a line connecting the previous position and the current position. To draw the line, the procedure uses GetDC to retrieve a display DC; then, as soon as drawing is complete and before returning from the message, the procedure uses the ReleaseDC function to release the display DC. As soon as the user releases the mouse button, the window procedure clears the flag, and the drawing stops (which sends the WM_LBUTTONUP message).

BOOL fDraw = FALSE;

POINT ptPrevious;

.

.

.

case WM_LBUTTONDOWN:

fDraw = TRUE;

ptPrevious.x = LOWORD(lParam);

ptPrevious.y = HIWORD(lParam);

return 0L;

case WM_LBUTTONUP:

if (fDraw) {

hdc = GetDC(hwnd);

MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);

LineTo(hdc, LOWORD(lParam), HIWORD(lParam));

ReleaseDC(hwnd, hdc);

}

fDraw = FALSE;

return 0L;

case WM_MOUSEMOVE:

if (fDraw) {

hdc = GetDC(hwnd);

MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);

LineTo(hdc, ptPrevious.x = LOWORD(lParam),

ptPrevious.y = HIWORD(lParam));

ReleaseDC(hwnd, hdc);

}

return 0L;

An application that enables drawing, as in this example, typically records either the points or lines so that the lines can be redrawn whenever the window is updated. Drawing applications often use a memory DC and an associated bitmap to store lines that were drawn by using a mouse.

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database