The Pen Class 2-D Graphics Programming with GDI+

163 DrawString Draws text. FillClosedCurve Draws a filled, closed curve. FillEllipse Draws a filled ellipse. FillPath Draws a filled figure whose shape is given by a GraphicsPath object. The GraphicsPath class is beyond the scope of this book. FillPie Draws a filled pie section. FillPolygon Draws a filled polygon see the DrawPolygon method earlier in this list. FillRectangle Draws a filled rectangle. FillRectangles Draws a series of filled rectangles. FillRegion Draws a filled figure whose shape is given by a Region object.

4.6.2 The Pen Class

Pen objects hold the settings used when drawing lines. All of the Graphics classs Draw...methods DrawArc, DrawBezier, etc. require that the caller supply a Pen object. The supplied Pen object determines the properties of the line used for drawing for example, its color, width, etc.. Ex am ple 4- 9 shows an OnPaint method that can be used to draw an ellipse on a user control or a form. It is similar to the code in Ex am ple 5- 6 in Chapt er 5 , but displays the ellipse a little smaller, and with only a border. The resulting display is shown in Figur e 4- 9 . Example 4-9. Drawing an ellipse on a form Protected Overrides Sub OnPaintByVal e As PaintEventArgs Dim pn As New PenMe.ForeColor Dim rect As Rectangle rect.X = Me.ClientRectangle.X + Me.ClientRectangle.Width \ 4 rect.Y = Me.ClientRectangle.Y + Me.ClientRectangle.Height \ 4 rect.Width = Me.ClientRectangle.Width \ 2 rect.Height = Me.ClientRectangle.Height \ 2 164 e.Graphics.DrawEllipsepn, rect pn.Dispose End Sub Figure 4-9. The ellipse drawn by the code in Ex a m ple 4 - 9 In Ex am ple 4- 9 , the Graphics classs DrawEllipse method is passed a Pen object, which determines the appearance of the line used for drawing the ellipse, and a rectangle, which defines the shape of the ellipse. The Pen class has four constructors. The constructor used in Ex am ple 4- 9 takes a parameter of type Color defined in System.Drawing. The color passed to the Pen class constructor in Ex am ple 4- 9 is the foreground color of the form Me.ForeColor. This is a nice touch ensuring that the ellipse will be drawn using whatever color is set as the foreground color of the form on which the ellipse is drawn. See Sect ion 4.6.4 later in this chapter for information on choosing and manipulating colors. Finally, note this line in Ex am ple 4- 9 : pn.Dispose By convention, objects that allocate scarce resources expose a Dispose method to allow the object client to tell the object to release its resources. When using any object that exposes a Dispose method as the Pen object does, the Dispose method must be called when the client code is finished using the object. If the Dispose method isnt called or if it isnt implemented, resources will be held longer than necessary, which may in turn result in resources being unavailable for other code that needs them. The .NET Framework provides a number of predefined pens through the properties of the Pens and SystemPens classes defined in the System.Drawing namespace. For example, the Blue property of the Pens class returns a Pen object whose color is set to Color.Blue . Thus, the following line of code draws a blue ellipse: e.Graphics.DrawEllipsePens.Blue, rect Similarly, the SystemPens classs WindowText property returns a Pen object whose color is set to the systems window text color. Using the standard pens provided by the Pens and SystemPens classes can be more efficient than instantiating new Pen objects. However, their properties such as line width cannot be altered. See Table 4- 1 , later in this chapter, for the list of Pen objects available through the Pens class. See Sect ion 4.6.4.1 in Sect ion 4.6.4 later in this chapter for the list of Pen objects available through the SystemPens class. 165 When working with a user-instantiated pen, you can modify the line that is drawn by setting properties of the Pen object. The code in Ex am ple 4- 10 sets the Pen objects Width property to widen the outline of the ellipse. The lines of code that differ from Ex am ple 4- 9 are shown in bold. The resulting display is shown in Figur e 4- 10 . Example 4-10. Setting Pen properties Protected Overrides Sub OnPaintByVal e As PaintEventArgs Dim pn As New PenMe.ForeColor pn.Width = 10 pn.DashStyle = Drawing.Drawing2D.DashStyle.Dash Dim rect As Rectangle rect.X = Me.ClientRectangle.X + Me.ClientRectangle.Width \ 4 rect.Y = Me.ClientRectangle.Y + Me.ClientRectangle.Height \ 4 rect.Width = Me.ClientRectangle.Width \ 2 rect.Height = Me.ClientRectangle.Height \ 2 e.Graphics.DrawEllipsepn, rect pn.Dispose End Sub Figure 4-10. The ellipse drawn by the code in Ex a m ple 4 - 1 0 Ex am ple 4- 10 sets the Pen objects Width and DashStyle properties to attain the effect shown in Figur e 4- 10 . The Width property is a value of type Single that determines the width of lines drawn with this pen. The default is 1. The unit of measurement is determined by the PageUnit property of the Graphics object in which the lines are drawn. The PageUnit property is of the enumeration type GraphicsUnit defined in the System.Drawing namespace. The values of GraphicsUnit that are appropriate for assignment to the PageUnit property are: Display Units are specified in 175 of an inch. Document Units are specified in 1300 of an inch. Inch Units are specified in inches. 166 Millimeter Units are specified in millimeters. Pixel Units are specified in pixels. Point Units are specified in points 172 of an inch. The DashStyle property of the Pen object determines the whether the line is solid or dashed, as well as the style of the dash. The DashStyle property is of the enumeration type DashStyle defined in the System.Drawing.Drawing2D namespace, which defines the following values: Custom Specifies a programmer-defined dash style. If this value is used, other properties of the Pen object control the exact appearance of the dashes in the line. Creating custom dash styles is not discussed in this book. Dash Specifies a dashed line. DashDot Specifies a line consisting of alternating dashes and dots. DashDotDot Specifies a line consisting of alternating dashes and two dots. Dot Specifies a dotted line. Solid Specifies a solid line. The standard dash styles are shown in Figur e 4- 11 . Figure 4-11. The standard DashStyle values 167

4.6.3 The Brush Class