Now let's take the next step up, from drawing lines to drawing figures. Windows' six functions for drawing filled figures with borders are listed in the chart below:
Function | Figure |
Rectangle | Rectangle with square corners |
Ellipse | Ellipse |
RoundRect | Rectangle with rounded corners |
Chord | Arc on the circumference of an ellipse with endpoints connected
by a chord |
Pie | Pie wedge on the circumference of an ellipse |
Polygon | Multisided figure |
PolyPolygon | Multiple multisided figures |
Windows draws the outline of the figure with the current pen selected in the device context. The current background mode, background color, and drawing mode are all used for this outline, just as if Windows were drawing a line. Everything we learned about lines also applies to the border around these figures.
The figure is filled with the current brush selected in the device context. By default, this is the stock object called WHITE_BRUSH, which means that the interior will be drawn as white. Windows defines six stock brushes: WHITE_BRUSH, LTGRAY_BRUSH, GRAY- BRUSH, DKGRAY_BRUSH, BLACK_BRUSH, and NULL_BRUSH (or HOLLOW_BRUSH). The first five of these brushes were used to color the client area of ROP2LOOK.
You can select one of the stock brushes into your device context the same way you select a stock pen. Windows defines HBRUSH to be a handle to a brush, so you can first define a variable for the brush handle:
HBRUSH hBrush ;
You can get the handle to GRAY_BRUSH by calling GetStockObject:
hBrush = GetStockObject (GRAY_BRUSH) ;
You can select it into the device context by calling SelectObject:
SelectObject (hdc, hBrush) ;
Now when you draw one of these figures, the interior will be gray.
If you want to draw a figure without a border, select the NULL_PEN into the device context:
SelectObject (hdc, GetStockObject (NULL_PEN)) ;
Or you can use the R2_NOP drawing mode:
SetROP2 (hdc, R2_NOP) ;
If you want to draw the outline of the figure but not fill in the interior, select the NULL_BRUSH into the device context:
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
You can also create customized brushes just as you can create customized pens. We'll cover that topic shortly.