How to: Use a Pen to Draw Rectangles
To draw rectangles, you need aobject and a object. The Graphics object provides the method, and the Pen object stores features of the line, such as color and width.
The following example draws a rectangle with its upper-left corner at (10, 10). The rectangle has a width of 100 and a height of 50. The second argument passed to theconstructor indicates that the pen width is 5 pixels.
When the rectangle is drawn, the pen is centered on the rectangle's boundary. Because the pen width is 5, the sides of the rectangle are drawn 5 pixels wide, such that 1 pixel is drawn on the boundary itself, 2 pixels are drawn on the inside, and 2 pixels are drawn on the outside. For more details on pen alignment, see.
The following illustration shows the resulting rectangle. The dotted lines show where the rectangle would have been drawn if the pen width had been one pixel. The enlarged view of the upper-left corner of the rectangle shows that the thick black lines are centered on those dotted lines.
Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires e, which is a parameter of the event handler.