Pen Object (GDI)
The Pen object is a Graphics Device Interface (GDI) object used to draw lines and curves. Pens define the color, width, and style of lines drawn by various GDI functions.
Overview
A pen is a fundamental drawing primitive in GDI. It's essentially a brush that is used to outline shapes, draw straight lines, and create stroked paths. When you draw a line or the border of a shape, GDI uses the currently selected pen in the device context (DC) to perform the drawing operation.
Pen Creation
Pens can be created using the following functions:
CreatePen: Creates a cosmetic pen with a specified style, width, and color. Cosmetic pens are typically 1 pixel wide and are the most common type.CreatePenIndirect: Creates a cosmetic pen using aLOGPENstructure.ExtCreatePen: Creates an extended pen, which can be cosmetic or geometric. Geometric pens can have widths greater than 1 pixel and support more complex styling.
DeleteObject function to free up system resources.
Pen Properties
A pen's appearance is defined by several properties:
- Style: Determines how the line is rendered (e.g., solid, dashed, dotted).
- Width: The thickness of the line in pixels. Cosmetic pens are always 1 pixel wide, while geometric pens can have arbitrary widths.
- Color: The RGB color value of the line.
Pen Styles
The following constants define the available pen styles:
PS_SOLID: A solid line.PS_DASH: A dashed line.PS_DOT: A dotted line.PS_DASHDOT: A dash-dot line.PS_DASHDOTDOT: A dash-dot-dot line.PS_NULL: No visible line.PS_INSIDEFRAME: For cosmetic pens, the line is drawn inside the frame of a closed shape.PS_GEOMETRIC: Used withExtCreatePento indicate a geometric pen.PS_COSMETIC: Used withExtCreatePento indicate a cosmetic pen.
Pen Width
For cosmetic pens created with CreatePen, the width is always 1 pixel. For pens created with ExtCreatePen, the width can be specified and applies to geometric pens.
Pen Color
The color of a pen is specified using an RGB color value (e.g., RGB(255, 0, 0) for red).
Using a Pen
To use a pen for drawing:
- Create the pen object using one of the creation functions.
- Select the pen into a device context (DC) using the
SelectObjectfunction. This returns the previously selected pen. - Perform your drawing operations (e.g.,
LineTo,Rectangle,Polyline). - When you are finished drawing, select the original pen back into the DC using
SelectObjectto restore the DC's state. - Delete the pen object using
DeleteObject.
SelectObject. This is crucial for preventing resource leaks and ensuring that other parts of your application or the system can use the original objects correctly.
Example
The following C++ code snippet demonstrates how to create a blue solid pen and draw a line:
HDC hdc = GetDC(hWnd); // Get the device context
HPEN hBluePen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); // Create a 2-pixel wide blue solid pen
HPEN hOldPen = (HPEN)SelectObject(hdc, hBluePen); // Select the new pen into the DC
// Draw a line
MoveToEx(hdc, 10, 10, NULL);
LineTo(hdc, 100, 100);
SelectObject(hdc, hOldPen); // Restore the original pen
DeleteObject(hBluePen); // Delete the pen object
ReleaseDC(hWnd, hdc); // Release the device context