Suite101

Win32 GDI Programming Techniques

Using SelectObject in Windows Programming for Drawing and Painting

© Guy Lecky-Thompson

Oct 17, 2008
Selecting objects in and out of context in the GDI, role of the GDI, painting, printing and metafiles in Win32 API GDI programming.

This article takes the programmer through the steps required to use the GDI (or Graphics Device Interface) in Win32 API programming to prepare for drawing commands. It explains the main points of the process:

  • Getting a handle to the device context;
  • Selecting GDI objects;
  • Using standard GDI objects;
  • Returning objects to Windows.

All of these steps are required to perform drawing operations in Windows, but using the GDI means that the whole process is made that much easier.

What is the Windows GDI?

The GDI (Graphics Device Interface) in Windows is an abstraction of the display device (screen, printer, etc.) that hides the implementation from the programmer. This allows the same set of API calls to be used for multiple devices - screens, printers, and metafiles.

The GDI operates through GDI objects (pens, brushes, etc.) that are used to paint the required graphical elements on the screen. These must be selected into the device context using the SelectObject function.

The Win32 API GDI SelectObject Function

The SelectObject function is defined as follows:

SelectObject ( HDC, HGDIOBJ );

The purpose is to select a handle to a GDI object into the device context so that it can be used. Once selected there are a few guidelines to follow:

  • Never delete a selected object;
  • Always delete objects that are no longer required;
  • Never select a bitmap into more than one device context at a time.

The HGDIOBJ objects are those that are returned by other GDI functions such as CreatePen, CreateBrush, CreateCompatibleBitmap etc.

Other Win32 API GDI Object Functions

Once the Win32 application has finished with an object, it can be deleted using the DeleteObject function. This simply takes the handle to the object, but must never be used when the object is still selected into the device context. Except for bitmaps, there is no real penalty for keeping the object available across multiple device contexts, except for the system limitation of 32K possible handles.

Another useful Win32 GDI API function is GetStockObject, which can be used to return a variety of system objects. A sidenote is that these follow system colors, therefore BLACK may have been reassigned to something else. The basic definition is:

GetStockObject ( int );

In the function, the int parameter refers to a system object, which includes brushes, pens and fonts. Some examples are:

  • BLACK_, WHITE_ and HOLLOW_ (PEN or BRUSH, i.e. BLACK_BRUSH)
  • ANSI_FIXED_FONT (i.e. Courier or similar);
  • ANSI_VAR_FONT (i.e. Arial or similar).

These objects should never be deleted with a call to DeleteObject.

Painting, Printing and Metafiles in Win32 Programming

The key to the above is in the device context. It is rare that the functions that perform the graphical operations are changed between the devices, so long as the correct layout (i.e. size and orientation) limitations have been followed, and the co-ordinates used in the drawing calls adjusted accordingly (scaled).

More recently, Microsoft has updated the standard GDI functions, with something called GDI+ (or GDI plus). This is a free download, and can be used to extend the basic functionality of the GDI to provide support, amongst other things, for:

  • JPEG and other image files
  • Better stretch, rotation and image manipulation functions
  • Advanced color management such as alpha channels

While it is still no match for proper graphics libraries like OpenGL and DirectX, it does go some way to providing a better interface for advanced image operations.


The copyright of the article Win32 GDI Programming Techniques in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 GDI Programming Techniques 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