Explore the core Windows API for 2D graphics and drawing.
The Graphics Device Interface (GDI) is a core component of the Microsoft Windows operating system that provides a standardized way for applications to draw graphics and text on the screen and printers. It handles operations such as drawing lines, curves, shapes, text, and bitmaps.
Rectangle(): Draws a rectangle.Ellipse(): Draws an ellipse.LineTo(): Draws a line.Polygon(): Draws a polygon.RoundRect(): Draws a rectangle with rounded corners.TextOut(): Writes a string of text at the specified coordinates.DrawText(): Formats and displays text within a rectangular block.CreateFont(): Creates a logical font.CreatePen(): Creates a new pen.CreateSolidBrush(): Creates a solid-colored brush.SelectObject(): Selects a GDI object into a device context.DeleteObject(): Deletes a GDI object.GDI supports various ways to define colors and brushes:
RGB(255, 0, 0) for red).CreateSolidBrush.GetStockObject(WHITE_BRUSH)).While GDI is a foundational API, Microsoft also developed GDI+. GDI+ offers a more modern, object-oriented approach to graphics, supporting features like anti-aliasing, gradients, and advanced image manipulation. For new development, GDI+ is often recommended, but understanding GDI remains crucial for legacy applications and lower-level graphics control.
Here's a conceptual example of drawing a red rectangle using GDI:
#include <windows.h>
// Assume hdc is a valid Device Context
HDC hdc;
RECT rect = { 10, 10, 100, 100 }; // Coordinates for the rectangle
HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0)); // Red brush
// Select the brush and draw the rectangle
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
// Restore the old brush and clean up
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);