|
|
|
Win32 GDI Drawing ShapesRectangles and Ellipses in the Windows API using Pens and Brushes
A Windows programming tutorial using the Win32 GDI API to draw rectangles and ellipses in the device context using pens and brushes.
Drawing shapes using the Win32 GDI API is a two stage process, once the device context has been obtained:
Since the shape will be drawn using only the selected pen and brush, it is important to make sure that the correct combination is chosen, depending on the desired effect. Selecting a Pen and BrushThe Pen is used to draw the outside edge of the shape, and the Brush is used to fill it. If PS_NULL, or BS_HOLLOW are used as the pen or brush styles, respectively, then the shape will have no edge (PS_NULL) or fill (BS_HOLLOW). These GDI objects can be useful for creating specific effects. When Pens or Brushes are overlapped during the drawing of a shape, the default behavior is to overwrite. However, this behavior can be changed by using the SetROP2 function to alter the raster operation mode. This alters the way that the colors already present, and the ones being drawn, are combined. Some common raster operations are:
Careful use of these operations can be sued to produce effects such as animation and non-destructive painting. The Rectangle and RoundRect FunctionsThe Rectangle function is used to draw a rectangle using the currently selected pen and brush, following a bounding box:
The four integers give the bounding box co-ordinates. They are client area co-ordinates, therefore, by default, the top-left of the client area is 0,0. The bounding box is therefore:
The corners of the rectangle are square, but they can be rounded by using the RoundRect function:
The last two integers give the width and height of the ellipse used to draw the quarter-circle rounded corners. A full ellipse can be drawn with the Ellipse function. The Ellipse FunctionLike the Rectangle function, the Ellipse function also takes four integers, and they give the four corners of the bounding rectangle that the ellipse is drawn within:
The ellipse is drawn such that the left, top, right and bottom edges of the bounding rectangle intersect with the 0, 90, 180, and 270 degree edges of the ellipse. The Z OrderThe Z order of the shapes that are drawn respect the drawing order. In other words, overlapping shapes will always be overlapped, each time the picture is re-drawn. If a diagram is being drawn, the programmer might like to use the bounding box calculations to make sure that individual components do not overwrite each other. Any shape drawn with the BS_HOLLOW brush will not be filled, and therefore will not blot out a shape drawn underneath (before) it. As mentioned, the exact effect of painting shapes on top of each other can be changed using the SetROP2 function.
The copyright of the article Win32 GDI Drawing Shapes in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 GDI Drawing Shapes in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|