A pen is an object that defines characteristics of a line. Pens are used to define color, line width, and line style (solid, dashed, and so on). Pens are used with almost all the drawing methods you'll learn about in this hour. Visual C# supplies a number of predefined pens, and you can also create your own. To create your own pen, use the following syntax: penvariable = new Pen(color, width); After a pen is created, you can set its properties to adjust its appearance. For example, all Pen objects have a DashStyle property that determines the appearance of lines drawn with the pen. Table 18.2 lists the possible values for DashStyle.
The enumeration for DashStyle is part of the Drawing.Drawing2D namespace. Therefore, to create a new, dark blue pen that draws a dotted line, you would use code like the following: Pen objMyPen = new Pen(System.Drawing.Color.DarkBlue, 3); objMyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; By the Way If System.Drawing was declared with a using statement, you would not need to use the fully qualified names here. The 3 passed as the second argument to create the new Pen defines the width of the pen in pixels. Visual C# includes many standard pens, which are available via the System.Drawing.Pens class, as in objPen = System.Drawing.Pens.DarkBlue; When drawing using the techniques discussed shortly, you can use custom pens or system-defined pensit's your choice. |