Home   Index   About
Ultimate Pack


Custom Search
Drawing Lines with the Mouse

The example in this section demonstrates how to track the mouse cursor. It contains portions of a window procedure that enables the user to draw lines in a window's client area by dragging the mouse.

When the window procedure receives a WM_LBUTTONDOWN message, it captures the mouse and saves the coordinates of the cursor, using the coordinates as the starting point of the line. It also uses the ClipCursor function to confine the cursor to the client area during the line drawing operation.

During the first WM_MOUSEMOVE message, the window procedure draws a line from the starting point to the current position of the cursor. During subsequent WM_MOUSEMOVE messages, the window procedure erases the previous line by drawing over it with an inverted pen color. Then it draws a new line from the starting point to the new position of the cursor.

The WM_LBUTTONUP message signals the end of the drawing operation. The window procedure releases the mouse capture and frees the mouse from the client area.

LRESULT APIENTRY MainWndProc(hwndMain, uMsg, wParam, lParam)

HWND hwndMain;

UINT uMsg;

WPARAM wParam;

LPARAM lParam;

{

HDC hdc; /* handle of device context */

RECT rcClient; /* client area rectangle */

POINT ptClientUL; /* client upper left corner */

POINT ptClientLR; /* client lower right corner */

static POINTS ptsBegin; /* beginning point */

static POINTS ptsEnd; /* new endpoint */

static POINTS ptsPrevEnd; /* previous endpoint */

static BOOL fPrevLine = FALSE; /* previous line flag */

switch (uMsg) {

case WM_LBUTTONDOWN:

/* Capture mouse input. */

SetCapture(hwndMain);

/*

* Retrieve the screen coordinates of the client area,

* and convert them into client coordinates.

/*

GetClientRect(hwndMain, &rcClient);

ptClientUL.x = rcClient.left;

ptClientUL.y = rcClient.top;

/*

* Add one to the right and bottom sides, because the

* coordinates retrieved by GetClientRect do not

* include the far left and lowermost pixels.

*/

ptClientLR.x = rcClient.right + 1;

ptClientLR.y = rcClient.bottom + 1;

ClientToScreen(hwndMain, &ptClientUL);

ClientToScreen(hwndMain, &ptClientLR);

/*

* Copy the client coordinates of the client area

* to the rcClient structure. Confine the mouse cursor

* to the client area by passing the rcClient structure

* to the ClipCursor function.

*/

SetRect(&rcClient, ptClientUL.x, ptClientUL.y,

ptClientLR.x, ptClientLR.y);

ClipCursor(&rcClient);

/*

* Convert the cursor coordinates into a POINTS

* structure, which defines the beginning point of the

* line drawn during a WM_MOUSEMOVE message.

*/

ptsBegin = MAKEPOINTS(lParam);

return 0;

case WM_MOUSEMOVE:

/*

* When moving the mouse, the user must hold down

* the left mouse button to draw lines.

*/

if (wParam & MK_LBUTTON) {

/*

* Retrieve a device context (DC) for the client

* area.

*/

hdc = GetDC(hwndMain);

/*

* The following function ensures that pixels of

* the previously drawn line are set to white and

* those of the new line are set to black.

*/

SetROP2(hdc, R2_NOTXORPEN);

/*

* If a line was drawn during an earlier

* WM_MOUSEMOVE message, draw over it. This erases

* the line by setting the color of its pixels to

* white.

*/

if (fPrevLine) {

MoveToEx(hdc, ptsBegin.x, ptsBegin.y,

(LPPOINT) NULL);

LineTo(hdc, ptsPrevEnd.x, ptsPrevEnd.y);

}

/*

* Convert the current cursor coordinates to a

* POINTS structure, and then draw a new line.

*/

ptsEnd = MAKEPOINTS(lParam);

MoveToEx(hdc, ptsBegin.x, ptsBegin.y,

(LPPOINT) NULL);

LineTo(hdc, ptsEnd.x, ptsEnd.y);

/*

* Set the previous line flag, save the ending

* point of the new line, and then release the DC.

*/

fPrevLine = TRUE;

ptsPrevEnd = ptsEnd;

ReleaseDC(hwndMain, hdc);

}

break;

case WM_LBUTTONUP:

/*

* The user has finished drawing the line. Reset the

* previous line flag, release the mouse cursor, and

* release the mouse capture.

*/

fPrevLine = FALSE;

ClipCursor(NULL);

ReleaseCapture();

return 0;

case WM_DESTROY:

PostQuitMessage(0);

break;

.

. /* Process other messages. */

.


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun