Device Contexts
A device context (DC) is a structure that contains information about the drawing attributes of a physical or virtual device. These attributes include the type of output device (such as a display screen, printer, or drawing surface), the drawing attributes (such as the pen and brush used for drawing lines and shapes), the current text font, the current drawing color, and the mapping mode that determines how coordinates are translated from page space to device space. An application uses a device context to draw on a device.
Overview
The Windows graphics device interface (GDI) provides a set of functions that applications can use to draw graphics and formatted text on a device. Every drawing operation is performed within the context of a device context. A device context is essentially a handle to a data structure that describes drawing attributes for a particular device.
You can obtain a device context in several ways:
- For the entire screen: Use the
GetDCfunction with a NULL window handle. - For a specific window: Use the
GetDCfunction with the handle of the target window. This provides a device context for the client area of the window. - For a specific window’s non-client area: Use the
GetWindowDCfunction. - For printing: Obtain a device context from a printer driver.
- From a device-independent bitmap (DIB) section: Create a memory device context and select a DIB into it.
Key Structures and Functions
Structures
DEVMODE: Contains device-specific initialization information for a printer driver.ENHMETAHEADER: Contains information about an enhanced metafile.METAFILEPICT: Describes a metafile.
Functions
CreateDC: Creates a device context for a specified device.DeleteDC: Deletes the specified device context (and releases any system memory it used).GetDC: Retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen.GetWindowDC: Retrieves a handle to a device context (DC) for the entire window, including the non-client area.ReleaseDC: Releases a device context, freeing it for use by other applications.SetMapMode: Sets the mapping mode for the specified device context.SetGraphicsMode: Sets the graphics mode for the specified device context.GetDeviceCaps: Retrieves the specified capabilities of a specified device.
Common Operations
Once you have a device context, you can perform various drawing operations. Here are some fundamental ones:
- Setting drawing attributes: Use functions like
SetPixel,MoveToEx,LineToto draw basic shapes and lines. - Selecting GDI Objects: You can select GDI objects like pens (for lines), brushes (for filling shapes), fonts (for text), and bitmaps into a device context using
SelectObject. - Drawing text: Use
TextOutorExtTextOutto render text. - Filling shapes: Functions like
Rectangle,Ellipse, andPolygonuse the currently selected brush to fill enclosed areas.
ReleaseDC or DeleteDC to avoid resource leaks.
Example: Drawing a Line
// Assume hdc is a valid device context handle
// Assume hWnd is the handle to the window
// Save the old pen
HPEN hOldPen = (HPEN)SelectObject(hdc, GetStockObject(PS_SOLID));
// Set the pen color to red
SetDCPenColor(hdc, RGB(255, 0, 0));
// Move to the starting point
MoveToEx(hdc, 10, 10, NULL);
// Draw a line to the end point
LineTo(hdc, 100, 100);
// Restore the old pen
SelectObject(hdc, hOldPen);
Mapping Modes
Device contexts use mapping modes to define the units of measurement and the origin of the coordinate system. Common mapping modes include:
MM_TEXT: Logical units are pixels. The Y-axis increases downwards.MM_LOMETRIC: Logical units are millimeters. The Y-axis increases upwards.MM_HIMETRIC: Logical units are tenths of a millimeter. The Y-axis increases upwards.MM_LOENGLISH: Logical units are inches. The Y-axis increases upwards.MM_HIENGLISH: Logical units are hundredths of an inch. The Y-axis increases upwards.MM_ISOTROPIC: The Y-axis is scaled independently of the X-axis. You can specify the aspect ratio of the device.MM_ANISOTROPIC: The Y-axis is scaled independently of the X-axis to match the aspect ratio of the physical device.
You can change the mapping mode using the SetMapMode function.
For more detailed information, please refer to the specific API documentation for each function and structure.