Saturday, November 5, 2011

Drawing Shapes in C programming

If text is so near can graphics be far behind? Now that we know how to draw text in a window let us now create a simple program that displays different shapes in a window. Instead of showing the entire program given below is the listing of OnPaint( ). The rest of the program is same as in the previous section. Here onwards I would be showing only the OnPaint( ) handler unless otherwise required
void OnPaint ( HWND hWnd )
{
HDC hdc ;
PAINTSTRUCT ps ;
HBRUSH hbr ;
HGDIOBJ holdbr ;
POINT pt[5] = { 250, 150, 250, 300, 300, 350, 400, 300, 320, 190 } ;
hdc = BeginPaint ( hWnd, &ps ) ;
hbr = CreateSolidBrush ( RGB ( 255, 0, 0 ) ) ;
holdbr = SelectObject ( hdc, hbr ) ;
MoveToEx ( hdc, 10, 10, NULL ) ;
LineTo ( hdc, 200, 10 ) ;
Rectangle ( hdc, 10, 20, 200, 100 ) ;
RoundRect ( hdc, 10, 120, 200, 220, 20, 20 ) ;
Ellipse ( hdc, 10, 240, 200, 340 ) ;
Pie ( hdc, 250, 10, 350, 110, 350, 110, 350, 10 ) ;
Polygon ( hdc, pt, 5 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
EndPaint ( hWnd, &ps ) ;
}
On execution of this program the window shown in Figure 18.2 appears.






For drawing any shape we need a pen to draw its boundary and a brush to paint the area enclosed by it. The DC contains a default pen and brush. The default pen is a solid pen of black color and the default brush is white in color. In this program we have used the default pen and a blue colored solid brush for drawing the shapes.
As before, we begin by obtaining a handle to the DC using BeginPaint( ) function. For creating a solid colored brush we need to call the CreateSolidBrush( ) API function. The second parameter of this function specifies the color of the brush. The function returns the handle of the brush which we have preserved in the hbr variable. Next we have selected this brush in the DC. The handle of the default brush in DC is collected in the holdbr variable.
Once we have selected the brush into the DC we are ready to draw the shapes. For drawing the line we have used MoveToEx( ) and LineTo( ) API functions. Similarly for drawing a rectangle we have used the Rectangle( ) function.
The RoundRect( ) function draws a rectangle with rounded corners. In RoundRect ( x1, y1, x2, y2, x3, y3 ), x1, y1 represents the x and y-coordinates of the upper-left corner of the rectangle. Likewise, x2, y2 represent coordinates of the bottom right corner of the rectangle. x3, y3 specify the width and height of the ellipse used to draw the rounded corners.
Note that rectangle and the rounded rectangle are drawn from x1, y1 up to x2-1, y2-1.
Parameters of Ellipse( ) specify coordinates of bounding rectangle of the ellipse.
The Pie( ) function draws a pie-shaped wedge by drawing an elliptical arc whose center and two endpoints are joined by lines. The center of the arc is the center of the bounding rectangle specified by x1, y1 and x2, y2. In Pie( x1, y1, x2, y2, x3, y3, x4, y4 ), x1, y1 and x2, y2 specify the x and y-coordinates of the upper left corner and bottom right corner respectively, of the bounding rectangle. x3, y3 and x4, y4 specify the x and y-coordinates of the arc’s starting point and ending point respectively.
In Polygon ( lpPoints, nCount ), lpPoints points to an array of points that specifies the vertices of the polygon. Each point in the array is a POINT structure. nCount specifies the number of vertices stored in the array. The system closes the polygon automatically, if necessary, by drawing a line from the last vertex to the first. Once we are through with drawing the shapes the old brush is selected back in the DC and then the brush created by us is deleted using DeleteObject( ) function


No comments:

Post a Comment