Working with Pens


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.

Table 18.2. Possible Values for DashStyle

Value

Description

Dash

Specifies a line consisting of dashes.

DashDot

Specifies a line consisting of a pattern of dashes and dots.

DashDotDot

Specifies a line consisting of alternating dashes and double dots.

Dot

Specified a line consisting of dots.

Solid

Specifies a solid line.

Custom

Specifies a custom dash style. The Pen object contains properties that can be used to define the custom line.


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.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net