Pens, Lines, and Rectangles

To draw lines with Windows GDI+ you need to create a Graphics object and a Pen object. The Graphics object provides the methods that actually do the drawing, and the Pen object stores attributes of the line, such as color, width, and style. Drawing a line is simply a matter of calling the DrawLine method of the Graphics object. The address of the Pen object is passed as one of the arguments to the DrawLine method. The following example draws a line from the point (4, 2) to the point (12, 6).

myGraphics.DrawLine(&myPen, 4, 2, 12, 6);

DrawLine is an overloaded method of the Graphics class, so there are several ways you can supply it with arguments. For example, you can construct two Point objects and pass references to the Point objects as arguments to the DrawLine method.

Point myStartPoint(4, 2);
Point myEndPoint(12, 6);
myGraphics.DrawLine(&myPen, myStartPoint, myEndPoint);

You can specify certain attributes when you construct a Pen object. For example, one Pen constructor allows you to specify color and width. The following example draws a blue line of width 2 from (0, 0) to (60, 30).

Pen myPen(Color(255, 0, 0, 255), 2);
myGraphics.DrawLine(&myPen, 0, 0, 60, 30);

The Pen object also has attributes, such as dash style, that you can use to specify features of the line. For example, the following example draws a dashed line from (100, 50) to (300, 80).

myPen.SetDashStyle(DashStyleDash);
myGraphics.DrawLine(&myPen, 100, 50, 300, 80);

You can use various methods of the Pen object to set many more attributes of the line. The Pen::SetStartCap and Pen::SetEndCap methods specify the appearance of the ends of the line; the ends can be flat, square, rounded, triangular, or a custom shape. The Pen::SetLineJoin method lets you specify whether connected lines are mitered (joined with sharp corners), beveled, rounded, or clipped. The following illustration shows lines with various cap and join styles.

illustration of a two lines demonstrating rounded and circular ends, rounded and mitered corners, and two arrow styles

Drawing rectangles with GDI+ is similar to drawing lines. To draw a rectangle, you need a Graphics object and a Pen object. The Graphics object provides a DrawRectangle method, and the Pen object stores attributes, such as line width and color. The address of the Pen object is passed as one of the arguments to the DrawRectangle method. The following example draws a rectangle with its upper-left corner at (100, 50), a width of 80, and a height of 40.

myGraphics.DrawRectangle(&myPen, 100, 50, 80, 40);

DrawRectangle is an overloaded method of the Graphics class, so there are several ways you can supply it with arguments. For example, you can construct a Rect object and pass a reference to the Rect object as an argument to the DrawRectangle method.

Rect myRect(100, 50, 80, 40);
myGraphics.DrawRectangle(&myPen, myRect);

A Rect object has methods for manipulating and gathering information about the rectangle. For example, the Inflate and Offset methods change the size and position of the rectangle. The Rect::IntersectsWith method tells you whether the rectangle intersects another given rectangle, and the Contains method tells you whether a given point is inside the rectangle.