Windows Graphics Device Interface (GDI)

The Graphics Device Interface (GDI) is a core component of the Microsoft Windows operating system responsible for presenting graphical information to users. GDI provides a set of functions that applications can use to draw lines, curves, text, and graphics primitives on various output devices such as displays, printers, and plotters.

This section of the MSDN Community provides a comprehensive reference to the GDI API, covering its functions, structures, data types, and concepts. Whether you are developing graphical applications, drivers, or system-level components, understanding GDI is crucial for effective graphics programming on Windows.

Key GDI Concepts

Core GDI Function Categories

Drawing Functions

Functions for drawing basic shapes, lines, and text.

  • Rectangle, Ellipse, LineTo
  • TextOut, ExtTextOut
  • MoveToEx, LineTo
  • Polygon, Polyline

GDI Object Management

Functions for creating, selecting, and deleting GDI objects.

  • CreatePen, CreateSolidBrush, CreateFont
  • SelectObject
  • DeleteObject
  • GetCurrentObject

Device Context Management

Functions for obtaining, manipulating, and releasing device contexts.

  • GetDC, CreateDC
  • GetDeviceCaps
  • ReleaseDC
  • SaveDC, RestoreDC

Bitmap and Pixel Operations

Functions for working with bitmaps and manipulating individual pixels.

  • CreateBitmap, CreateCompatibleBitmap
  • BitBlt, StretchBlt
  • SetPixel, GetPixel

Path and Region Management

Functions for defining and manipulating complex graphical areas.

  • BeginPath, EndPath, StrokePath
  • CreatePolygonRgn, CreateRectRgn
  • CombineRgn

Example Usage (Conceptual)

Here's a simplified conceptual example of how you might use GDI to draw a red line:


HDC hdc = GetDC(NULL); // Get a device context for the screen

if (hdc != NULL) {
    // Create a red pen
    HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
    if (hPen != NULL) {
        // Select the pen into the device context
        HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);

        // Draw a line from (10, 10) to (100, 100)
        MoveToEx(hdc, 10, 10, NULL);
        LineTo(hdc, 100, 100);

        // Restore the original pen
        SelectObject(hdc, hOldPen);

        // Clean up the pen
        DeleteObject(hPen);
    }

    // Release the device context
    ReleaseDC(NULL, hdc);
}