Windows Graphics Device Interface (GDI)
The Windows Graphics Device Interface (GDI) is a core component of the Microsoft Windows operating system that provides a consistent way for applications to interact with graphics hardware and display output. GDI handles the rendering of text, shapes, and images on various output devices such as monitors, printers, and plotters. It offers a rich set of functions for drawing, text manipulation, and device management.
Key Concepts
- Device Context (DC): A structure that contains information about a drawing surface and the attributes of the graphics output, such as the drawing pen, brush, font, and background color.
- GDI Objects: Resources used for drawing, including Pens (for lines), Brushes (for filling areas), Bitmaps (for images), and Fonts (for text).
- Coordinate Systems: GDI uses a logical coordinate system that applications use for drawing, which is then mapped to the physical coordinate system of the device.
Core Functions
Drawing Functions
These functions are used to draw lines, shapes, and curves.
LineToDraws a line from the current position to a specified point.RectangleDraws a rectangle.EllipseDraws an ellipse.PolygonDraws a polygon.MoveToExUpdates the current position.
// Example: Drawing a red line
HDC hdc = GetDC(hwnd); // Get device context for the window
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); // Create a red pen
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); // Select the pen into the DC
MoveToEx(hdc, 10, 10, NULL); // Set the starting point
LineTo(hdc, 100, 100); // Draw the line
SelectObject(hdc, hOldPen); // Restore the old pen
DeleteObject(hPen); // Clean up the pen
ReleaseDC(hwnd, hdc); // Release the device context
Text Functions
These functions enable drawing text on the screen.
TextOutDraws a string of characters at a specified location.SetTextColorSets the text color.SetBkColorSets the background color for text.CreateFontCreates a logical font.GetTextExtentPoint32Calculates the dimensions of a text string.
Object Management
Functions for creating, selecting, and deleting GDI drawing objects.
CreatePenCreates a pen.CreateSolidBrushCreates a solid brush.SelectObjectSelects an object into a device context.DeleteObjectDeletes a GDI object.
Device Management
Functions for managing device contexts.
GetDCRetrieves a handle to a device context (DC) for a client area of a specified window.ReleaseDCReleases a device context, freeing it for use by other applications.CreateCompatibleDCCreates a memory device context that is compatible with the specified device.
For more detailed information and a complete list of functions, please refer to the GDI Programming Guide.