|
|
Win32 CreateDC and GDI FunctionsWin32 GDI API Programming and Handling Device Contexts and BitmapsWindows 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:
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.
The simplest of these is the GetDC function. Using the Win32 API Function GetDCThe GetDC function is defined as follows:
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 ReleaseDCThe ReleaseDC function is defined as follows:
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 DeleteDCThe 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:
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:
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.
The copyright of the article Win32 CreateDC and GDI Functions in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 CreateDC and GDI Functions in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|