Win32 CreateDC and GDI Functions

Win32 GDI API Programming and Handling Device Contexts and Bitmaps

Windows programming tutorial introducing CreateDC, CreateCompatibleDC, and HDC to create and manage Win32 GDI device contexts and bitmaps using Windows functions.

A device context is an area of memory in which the Windows programmer can use GDI functions to draw objects (lines, shapes, text, etc.) The GDI (Graphical Device Interface) is important as it provides a device independent interface for using the drawing functions. The same functions are used for screens, printers and bitmaps, including those used to get, and describe, the device context.

The Win32 GDI API contains several functions and structures for processing device contexts. The data structures are:

  • HDC - the handle to a device context;
  • PAINTSTRUCT - the painting structure used in WM_PAINT processing.

In principle, if all drawing is performed in the message loop, then the device context and paint structure are available during the processing of the WM_PAINT message, as outlined in the Win32 Drawing WM_PAINT Processing article.

Sometimes, however, it is necessary to make changes directly to the application (or dialog) window, or draw on a bitmap, or printer device context. For this, the following functions are used.

  • GetDC - retrieve the device context associated with a window;
  • ReleaseDC - release the device context back to windows;
  • CreateCompatibleDC - create a compatible device context, usually off-screen.

The simplest of these is the GetDC function.

Using the Win32 API Function GetDC

The GetDC function is defined as follows:

  • HDC GetDC ( HWND );

The function returns a handle to the device context that is usually represented by the client area of the window hand passed to it. To obtain a handle to the entire window, including title bar, menus and scroll bars, the GetWindowDC is used instead, which operates in the same way to GetDC.

Once painting has been completed, the programmer must call the Win32 API function ReleaseDC.

Using the Win32 API Function ReleaseDC

The ReleaseDC function is defined as follows:

  • int ReleaseDC ( HWND, HDC );

The return value will be set to 1 if the device context has been successfully released. This function is only for use with device contexts that have been obtained, and not those that have been created by the programmer.

The easiest way to create a device context is with the Win32 API CreateCompatibleDC function.

Using the Win32 API Functions CreateCompatibleDC and DeleteDC

The CreateCompatibleDC function is used to create a device context (DC) without an associated window. This is useful for drawing onto bitmaps and other off-screen resources. The function is defined as follows:

  • HDC CreateCompatibleDC ( HDC );

Clearly, to use the function, the programmer must first have accessed an existing DC, using an appropriate method such as the aforementioned GetDC Win32 API GDI function.

Once the application has finished with the custom DC, it must call the DeleteDC function:

  • BOOL DeleteDC ( HDC );

Since the DC is not associated directly with a window, there is no window handle to pass to the function, which returns TRUE if the DC has been deleted successfully.

Guy Lecky-Thompson, Self Portrait

Guy Lecky-Thompson - Guy W. Lecky-Thompson is the author of several technical and non-technical books, and writer at large. He has written for Dr. Dobbs ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 0+10?
Advertisement
Advertisement