Drawing Win32 GDI API Programming: Using Windows APIs to Draw on the Screen without DirectX or OpenGL


This Win32 GDI API programming tutorial is aimed at programmers who need to be able to draw on the screen using native Win32 GDI API calls, without learning the ins and outs of third party drawing libraries such as OpenGL or DirectX. While support for these is excellent under Win32, they are only really applicable where performance is paramount:

  • games programming
  • high-end CAD programming
  • video / photo manipulation
  • etc.

For most tasks, the Win32 GDI and GDI+ APIs are perfectly adequate, if a little slow when processing large amounts of drawing commands. The advantage, however outweighs this small deficit : if the programmer can draw on the screem, they can draw anywhere that the OS knows about:

  • the screen
  • a printer
  • a plotter
  • a metafile or enhanced metafile
  • an offscreen bitmap
  • etc.

This makes the Win32 GDI API very useful for a wide variety of applications.

Win32 Drawing WM_PAINT Processing

Most drawing is done in response to a request from Windows to repaint the client area of the screen. The programmer is informed of this necessity by the reception of a WM_PAINT message in their Windows message processing loop.

For more about processing this message, the reader should consult the Win32 Drawing WM_PAINT Processing article. Once painting has begun, the programmer must direct all output to a device context (or DC).

Getting a Device Context

The Device Context handle points to a specific definition of a device that has been selected via a device driver. Luckily for the programmer, the default DCs – screen, printer, etc. – are easily selected. For more information about how to choose and select a DC, the reader should read the Win32 CreateDC and GDI Functions article.

The DC abstracts the actual capabilities of the device into the Windows API, so that it can be manipulated using standard GDI commands, which usually operate on objects.

The Role of GDI and GDI Objects

GDI stands for Graphics Device Interface, and provides a consistent interface to graphics programming, no matter the target DC (device context).

It is important to remember that manipulation of the GDI is mainly performed through the selection of dedicated objects. The two primary objects are the Brush and Pen.

Brushes and Pens in the Win32 GDI API

A GDI Brush is used to paint the interior of a shape, while a Pen is used to draw the outside, or plot lines. They can have several styles, which are discussed in the Win32 GDI Programming with Pens article.

If the programmer tries to draw a line without selecting at least a default pen, nothing will happen. GDI objects must be selected into a valid Device Context in order to be used.

Using Bitmaps in Windows

Device Contexts can also be virtual (compatible), existing only in memory. The only way to ever see the effect of calling Windows GDI commands on such DCs is to select a bitmap into them (again, a compatible one) and then copy that bitmap to the visible DC.

This is very good for simple animation, as it is far faster to draw to a bitmap in memory than it is to draw directly to the screen. So-called offscreen drawing is covered further in the Win32 Drawing and Bitmaps How-To article.

The Win32 GDI API is very powerful, and with the additional image processing capabilities of GDI+, there is very little reason (for simple applications) to resort to third party libraries like DirectX or OpenGL.

However, budding game programmers should take note that these technologies offer far more than the ability to draw a line or a filled shape in a given pen. They are geared towards game development and can handle 3D, shading, and aother advanced techniques which fall well outside the possibilities offered by the native Win32 GDI API.