Graphics Device Interface (GDI)
The Graphics Device Interface (GDI) provides a device-independent way to represent graphical output. It allows applications to draw lines, curves, text, and graphics primitives on various output devices, such as screens and printers, without needing to know the specific details of the underlying hardware.
Key Concepts
Understanding GDI involves familiarizing yourself with several core concepts:
- Device Context (DC): A data structure that contains information about the drawing surface (like a window or printer) and the attributes of the drawing object (pen, brush, font, etc.).
- Pens: Used for drawing lines, outlines of shapes, and borders.
- Brushes: Used for filling areas and drawing text.
- Fonts: Define the typeface, size, and style of text to be drawn.
- Paths: A sequence of lines and curves that can be stroked or filled.
Core Functions
Below is a list of commonly used GDI functions. Click on a function name for detailed documentation.
CreatePen
Creates a new pen with the specified style, width, and color.
fnPenStyle: The pen style. Can be PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT, PS_NULL, or PS_GEOMETRIC.nWidth: The width of the pen in logical units.crColor: The RGB color of the pen.
If the function succeeds, the return value is a handle to the newly created pen object. If the function fails, the return value is NULL.
Rectangle
Draws a rectangle using the current pen and fills it using the current brush.
hdc: Handle to the device context.nLeftRect: The x-coordinate of the upper-left corner of the rectangle.nTopRect: The y-coordinate of the upper-left corner of the rectangle.nRightRect: The x-coordinate of the lower-right corner of the rectangle.nBottomRect: The y-coordinate of the lower-right corner of the rectangle.
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
The rectangle is drawn with the current pen and filled with the current brush. If the pen is a cosmetic pen, the rectangle outline is drawn using the current color. If the pen is a geometric pen, the rectangle outline is drawn using the pen's attributes. If the brush is NULL, the rectangle is not filled.
TextOut
Writes a string of text at the specified location.
hdc: Handle to the device context.nXStart: The x-coordinate of the starting position of the string.nYStart: The y-coordinate of the starting position of the string.lpString: Pointer to the string to be written.nCount: The number of characters in the string.
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
GetDC or for the entire screen using GetDC(NULL). Remember to release the DC when you are finished using ReleaseDC.