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
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
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 …