|
|
|
Win32 GDI Programming TechniquesUsing SelectObject in Windows Programming for Drawing and PaintingSelecting 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:
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 FunctionThe 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:
The HGDIOBJ objects are those that are returned by other GDI functions such as CreatePen, CreateBrush, CreateCompatibleBitmap etc. Other Win32 API GDI Object FunctionsOnce 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:
These objects should never be deleted with a call to DeleteObject. Painting, Printing and Metafiles in Win32 ProgrammingThe 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:
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.
|
|
|
|
|
|
|
|