Using Brushes and Pens in GDI API

Win32 GDI Tutorial Explaining How To Use the API to Draw with GDI

© Guy Lecky-Thompson

Jan 29, 2009
This Win32 GDI API Windows drawing tutorial defines brushes, pens and other objects , and how to use them to draw simple hollow and filled shapes onto a Device Context.

The Win32 GDI API has many useful features for drawing shapes and plotting lines, including line segments, curves, and shapes of an arbitrary number of sides. The topics covered in this overview are:

  • Brushes (i.e. HBRUSH objects)
  • Pens (i.e. HPEN objects)
  • Lines and Paths

Those readers who are not familiar with the basic terminology above should read the Drawing Win32 GDI API Programming article first to get the most from the subjects covered here.

There are two kinds of GDI objects - logical and system - the former of which can be created programatically by using the GDI API. The latter cannot, but are always there if the programmer needs them.

It is worth making a small note about the use of color in GDI programming. Most pens and brushes will need to be associated with a COLORREF value, which is equivalent to the closest screen match (actually DC match) to a specific RGB color. It is returned by the following function call:

COLORREF RGB ( nRed, nGreen, nBlue )

This COLORREF object (value) can then then be used to create, for example, a pen.

Pen Objects in the Win32 GDI API

More detail on the way to allocate and deploy these Pens is covered in the Win32 GDI Programming with Pens article. All that the pen is designed to do is allow the programmer to draw a line, in a given color and style:

  • Dashed
  • Dotted
  • Dash-Dotted
  • Solid

In order to draw filled shapes, it is necessary to define both a pen and a brush.

Brush Objects in the Win32 GDI API

Like a pen, a brush can have both color and style information. It can also have various types of hatching applied; and for this reason there are actually different kinds of brushes that can be created. A complete guide to using Brushes in the Win32 GDI API is covered in the Creating Brushes in the Win32 API article.

The main reason to define the brush is to draw filled shapes.

Drawing Shapes with the Win32 GDI API

There are many shapes available to programmers using the GDI API:

  • Ellipses
  • Polygons (of an arbitrary number of sides)
  • Rectangles
  • Rounded rectangles
  • etc.

Each can be outlined with a pen, and filled with a brush - including transparent brushes or pens, known as hollow objects. For more information on how to draw both filled and hollow shapes, the reader should consult the Win32 GDI Drawing Shapes article.

Using Lines and Paths in the Win32 GDI API

The last subject in this GDI primer is Lines and Paths. These are both ways of drawing shapes using segments.

Actually using lines and paths is deceptively easy, and is covered in full in the Win32 GDI Path Functions article. Paths are a way of turning complex graphical shapes into a set of lines that can be described in terms of their co-ordinates and subsequently scaled, rotated, and otherwise manipulated.

With these primitives, and a little ingenuity, it is possible to create very complex shapes using the Win32 GDI API. While probably not fast enough for arcade games, so long as the programmer is sensible about how much drawing they do (i.e. by only updating parts of the screen that need it, and using memory bitmaps), the Win32 GDI API should be enough for non real-time applications.


The copyright of the article Using Brushes and Pens in GDI API in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Using Brushes and Pens in GDI API in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo