|
|
Win32 GDI Drawing Polygons ShapesWindows Graphics Programming Tutorial for PolyLine and PolyDrawHow to draw shapes in Windows with an arbitrary number of sides or vertices using the PolyLine and PolyDraw Win32 GDI API functions.
The Polyline and PolyDraw functions both enable the programmer to create shapes based on an array of points, which is more convenient, and faster, than using multiple MoveToEx and LineTo function calls. In addition, PolyDraw has several modes for drawing disjointed figures, bezier curves, and so on. The Arrays of POINTsBoth Win32 polygon drawing functions in the GDI API use an array of POINTs to represent the points in the drawing space that are the corners (connecting points) of the polygon. Since the Win32 API is unable to determine the number of points in the array, this has to be provided too. The array of POINTs is passed by reference: CONST POINT * lppt To specify a square, 100 pixels to a side, starting at position 25, 75 the following code might be used: POINT ptArray[5]; ptArray[0].x = 25; ptArray[0].y = 75; ptArray[1].x = 125; ptArray[1].y = 75; ptArray[2].x = 125; ptArray[2].y = 175; ptArray[3].x = 25; ptArray[3].y = 175; ptArray[4].x = 25; ptArray[4].y = 75; This can then be passed to the Polyline function. The Win32 GDI API Polyline FunctionThe Polyline function draws a series of lines, to connect the points that are specified in the POINT array that is passed as a parameter to the function call. The general form for the function is as follows: BOOL Polyline ( HDC hdc, CONST POINT * lppt, int nPoints ); It does not use the current position (nor does it update it). For this reason, it is necessary to specify the start/end point twice in order to draw a closed shape. Even if a closed shape is the result, the polygon is not filled. The lines are drawn in the currently selected pen. If the ptArray from the above code sample is passed to the Ployline function, then the result will be a square. For more flexibility, including curved lines (using Bezier curves), the iPolyDraw function can be used. The Win32 GDI API PolyDraw FunctionThe Polydraw function can be used to create:
Despite the fact that the figure can be closed automatically, it will still not be filled. However, the functionality is helpful, in that the programmer need not specify the start/end point twice. The PolyDraw function also takes a second array: BOOL PolyDraw ( HDC hdc, CONST POINT * lppt, CONST BYTE nTypes, int nPoints ); The nTypes array contains identifiers that can be one of:
This last is used in conjunction (using the logical OR operator, |) with one of the first two, to indicate that it is the last point, and that the figure should be closed by drawing a line to the last point that was identified with the PT_MOVETO identifier. Drawing with Bezier CurvesThere is a special identifier in the array of identifiers passed to the PolyDraw function : PT_BEZIERTO. The Bezier curve is defined in terms of three POINT entries in the array. The first two are control points, that specify the curve, and the last one is the end point. The current position is used as the starting point. The programmer will need to experiment with these a little in order to find out how to draw the curves accurately.
The copyright of the article Win32 GDI Drawing Polygons Shapes in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 GDI Drawing Polygons Shapes in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|