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
- Device Context (DC): A structure that contains information about the drawing surface and the drawing attributes. All drawing operations are performed through a device context.
- Pens, Brushes, Fonts, Bitmaps: GDI objects that define the characteristics of drawing operations.
- Drawing Functions: A comprehensive set of functions for rendering various graphical elements.
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:
GetDC()
: Retrieves a device context for a client area of a specified window.BeginPaint()
: Prepares a window for painting and retrieves a device context.EndPaint()
: Marks the end of painting for a window.CreateCompatibleDC()
: Creates a memory device context that is compatible with a specified device context.DeleteDC()
: Deletes a device context.
Pens
Pens are used to draw lines and borders. They have a color, style (solid, dashed, etc.), and width.
CreatePen()
: Creates a pen.SelectObject()
: Selects an object (like a pen) into a device context.DeleteObject()
: Deletes a GDI object.
Brushes
Brushes are used to fill areas, such as the interior of shapes or text backgrounds.
CreateSolidBrush()
: Creates a solid-color brush.CreatePatternBrush()
: Creates a brush with a specified bitmap pattern.SelectObject()
: Selects a brush into a device context.
Fonts
Fonts define the appearance of text.
CreateFont()
: Creates a logical font.CreateFontIndirect()
: Creates a font based on aLOGFONT
structure.SelectObject()
: Selects a font into a device context.TextOut()
: Draws a string of text.ExtTextOut()
: Draws a string of text with additional formatting options.
Bitmaps
Bitmaps are raster graphics, essentially arrays of pixels.
CreateBitmap()
: Creates a monochrome bitmap.CreateBitmapIndirect()
: Creates a bitmap based on aBITMAP
structure.CreateCompatibleBitmap()
: Creates a bitmap that is compatible with the device context.LoadBitmap()
: Loads a bitmap resource from an executable file.BitBlt()
: Copies a block of pixels from a source device context to a destination device context.
Common Drawing Functions
MoveToEx()
: Sets the current position for the specified device context.LineTo()
: Draws a line from the current position to a specified point.Rectangle()
: Draws a rectangle.Ellipse()
: Draws an ellipse.RoundRect()
: Draws a rectangle with rounded corners.Polygon()
: Draws a polygon.Polyline()
: Draws a series of connected lines.
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