Windows API Documentation

Graphics Device Interface (GDI)

The Graphics Device Interface (GDI) is a core Windows component that provides a device-independent way for applications to interact with graphical output devices such as monitors and printers. It allows applications to draw lines, curves, text, and images without needing to know the specific capabilities of the underlying hardware.

Key Concepts

Core GDI Objects

Device Context (DC)

A DC is essential for any drawing operation. You obtain a DC for a specific window or screen, and then use it to call drawing functions. Common functions include:

Pens

Pens are used to draw lines and borders. They have a color, style (solid, dashed, etc.), and width.

Brushes

Brushes are used to fill areas, such as the interior of shapes or text backgrounds.

Fonts

Fonts define the appearance of text.

Bitmaps

Bitmaps are raster graphics, essentially arrays of pixels.

Common Drawing Functions

Example: Drawing a Red Line

The following C++ code snippet demonstrates how to draw a red line on a window using GDI:


HDC hdc = GetDC(hWnd); // Get device context for the window
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // Create a red pen (2 pixels wide)
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); // Select the new pen into the DC

MoveToEx(hdc, 50, 50, NULL); // Set the starting point
LineTo(hdc, 200, 150); // Draw the line to the endpoint

SelectObject(hdc, hOldPen); // Restore the original pen
DeleteObject(hPen); // Delete the GDI pen object
ReleaseDC(hWnd, hdc); // Release the device context
            

Related Topics