Page 216 - DCAP408_WEB_PROGRAMMING
P. 216
Windows Programming
Notes 13.1.2 Displaying Pixels
You can drawn single pixels to the screen using the following function:
SetPixel( HDC hdc, int X, int Y, COLORREF crColor);
Again it requires a handle to a device context (HDC) and a screen position. Additionally the
colour you want to set the pixel to can be provided. This needs to be given in a Win32 type
COLORREF. Fortunately, we can use a macro (RGB) to convert from three Red, Green and Blue
values to a COLORREF type. Each colour component can range from 0 to 255.
To display a red pixel at screen co-ordinates 100,100 we would write:
SetPixel(hdc, 100, 100, RGB(255,0,0));
Task In a group of four analyze why GDI can be used in all Windows-based applications.
13.1.3 Drawing Lines
There are a number of functions for drawing lines using GDI. You can use MoveTo and LineTo
to position a pen on the screen and draw a line to another position. You can also use PolyLine,
PolyPolyLine, Arc, etc. To explore which ones are available look in the MSDN help. We will
describe here the use of PolyLine.
PolyLine draws lines between points defined in an array you pass to the function. The function
definition is:
Polyline(HDC hdc, CONST POINT *lppt, int cPoints);
This takes the device handle, an array of points and how many points there are in the array. It
draws lines between each point. POINT is a structure with member variables x and y.
To draw a simple diagonal line we could do this:
POINT pntArray[2];
pntArray[0].x=10;
pntArray[0].y=10;
pntArray[1].x=100;
pntArray[1].y=100;
Polyline(hdc, pntArray, 2);
!
Caution The color and style of the line can be changed by using pens.
13.1.4 Drawing Filled Shapes
You can draw filled rectangles using FillRect and filled ellipses using Ellipse. Also, not covered
here, you can draw filled polygons, pies, rounded rectangles etc. Again look in the MSDN help
for details.
210 LOVELY PROFESSIONAL UNIVERSITY