[Dev-C++] Drawing Lines (Let's put an end to this thread)...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David N. <dne...@mi...> - 2001-11-05 20:57:08
|
OK, it's obvious that the person that coded:
> case WM_PAINT:
> SetPixel(GetDC(hwnd),100,100,0x00FF0000);
> SetPixel(GetDC(hwnd),101,100,0x00FF0000);
> SetPixel(GetDC(hwnd),102,100,0x00FF0000);
> SetPixel(GetDC(hwnd),103,100,0x00FF0000);
> SetPixel(GetDC(hwnd),104,100,0x00FF0000);
> break;
is really a newbie. Multiple calls to GetDC() with no handle reuse, no =
ReleaseDC() calls, and trying to plot pixels rather than the =
MoveToEx()/LineTo() calls.
In case he's wondering, here's the code to do what he wanted:
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbr,oldBrush;
hdc=3DBeginPaint(hwnd,&ps); // get the device context during a =
WM_PAINT message
hbr =3D CreateSolidBrush(RGB(0,0,0xff)); // create a brush for =
drawing
oldBrush =3D (HBRUSH) SelectObject(hdc,hbr); // select the brush =
into the DC
MoveToEx(hdc,100,100,NULL); // Move to the specified position
LineTo(hdc,104,100); // draw the line
SelectObject(hdc,oldBrush); // select the old brush back to the =
dc
DeleteObject(hbr); // delete our temporary brush
EndPaint(hwnd,&ps); // end the paint session
}
break;
Now all of these functions are documented either in the Win32 SDK help =
file previously mentioned at =
ftp://ftp.cs.virginia.edu/pub/lcc-win32/win32hlp.exe site.
Of course there's always the definitive source at Microsoft's MSDN site: =
http://msdn.microsoft.com/library/ where you'll find all of the info =
from the Win32 help file and then some.
I would suggest that the person trying to figure out how to plot pixels =
should pick up an introductory book to programming Win32 in C/C++ =
(Charles Petzold did a marvelous job in Programming Windows 95; it's C & =
C++ (but avoids MFC), explains all about how to use the Windows API for =
drawing), and then come back when he runs into a problem...
Dave
|