Drawing shapes using the Win32 GDI API is a two stage process, once the device context has been obtained:
- Select an appropriate Pen and Brush;
- Call the relevant function to draw the shape.
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 Brush
The 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:
- R2_COPYPEN - the default; result is the same as the pen/brush color
- R2_MASKPEN - result is the colors that are common to both source and destination
- R2_XORPEN - result is those colors that are in the source or destination, but not both
Careful use of these operations can be sued to produce effects such as animation and non-destructive painting.
The Rectangle and RoundRect Functions
The Rectangle function is used to draw a rectangle using the currently selected pen and brush, following a bounding box:
- Rectangle ( HDC, int, int, int, int )
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:
- Left, Top : x and y co-ordinates of the top-left corner of the rectangle
- Right, Bottom : x and y co-ordinates of the bottom-right corner of the rectangle
The corners of the rectangle are square, but they can be rounded by using the RoundRect function:
- Rectangle ( HDC, int, int, int, int, int, int )
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 Function
Like 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:
- Ellipse ( HDC, int, int, int, int, int, int )
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 Order
The 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.
Join the Conversation