The System.Drawinging.Pen and System.Drawing.Font classes represent pen and font objects in GDI+.
The Pen Class
A pen draws a line of specified width and style. You always use Pen constructor to create a pen. The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush.
Initializes a new instance of the Pen class with the specified color. The Color is the only one parameter passed in constructor as an argument.
public
Pen(Color);
Initializes a new instance of the Pen class with the specified Brush. The Brush is the only one parameter passed in constructor as an argument.
public
Pen(Brush);
Initializes a new instance of the Pen class with the specified Brush and width.
public
Pen(Brush, float);
Initializes a new instance of the Pen class with the specified Color and Width.
public
Pen(Color, float);
Here is one example:
Pen pn = new Pen( Color.Blue );
or
Pen pn = new Pen( Color.Blue, 100 );
Some of its most commonly used properties are Alignment, Brush, Color, and Width, which are self-explanatory.
The Font Class
The Font class defines a particular format for text such as font type, size, and style attributes. You use font constructor to create a font.
Initializes a new instance of the Font class with the specified attributes.
public
Font(string, float);
Initializes a new instance of the Font class from the specified existing Font and FontStyle.
public
Font(Font, FontStyle);
Where FontStyle is an enumeration, which include values Bold, Italic, Regular, StrikeOut, and Underline, which are self-explanatory. Here is one example:
Graphics g ;
Font font = new Font("Times New Roman", 26);
Some of its most commonly used properties are:
Bold |
Gets a value indicating whether this Font is bold. |
FontFamily |
Gets the FontFamily of this Font. |
Height |
Gets the height of this Font. |
Italic |
Gets a value indicating whether this Font is Italic. |
Name |
Gets the face name of this Font. |
Size |
Gets the size of this Font. |
SizeInPoints |
Gets the size, in points, of this Font. |
Strikeout |
Gets a value indicating whether this Font is strikeout (has a line through it). |
Style |
Gets style information for this Font. |
Underline |
Gets a value indicating whether this Font is underlined. |
Unit |
Gets the unit of measure for this Font. |
Working with Fonts and FontFamily
Although we've already discussed Fonts in the previous section of this chapter but there is more to be discussed about fonts.
The System.Drawing.Font class represents a font type. For example,
Font greenSolid = new Font("Verdana", 14);
Creates a font type verdana with size 14. You can also use a FontStyle as an argument when constructing a font. The below line creates a font of Tahoma with different styles.
Font redStyle = new Font("Tahoma", 20, FontStyle.Bold|FontStyle.Italic|FontStyle.Underline);
g.DrawString("Text on the Screen", greenSolid, new SolidBrush(Color.Green), 10,10);
g.DrawString("Red Text", redStyle, new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red), 50,40);
The FontStyle Enumeration defines these styles.
Table 11-6. FontStyle Enumeration
Member |
Description |
Bold |
Bold Text. |
Italic |
Italic Text |
Regular |
Regular Text. |
StrikeOut |
Text with a line in middle. |
Underline |
Underline Text. |
FontFamily is another class we'd like to discuss before moving to the next topic. System.Drawing.FontFamily class represents a font family, which is used to work with similar kind of fonts with different style variations. For example, Tahoma font can have different styles and size.
FontFamily tahomaFmly = new FontFamily("Tahoma");
Here tahomaFmly represents the Tahoma font family.
In the below sample example, green28 and red14Italic uses same font family to create a Tahoma font with different styles.
FontFamily fontFmly = new FontFamily("Tahoma");
Font green28 = new Font(fontFmly, 28);
Font red14Italic = new Font(fontFmly, 14, FontStyle.Italic);
g.DrawString("Text on the Screen", green28, new SolidBrush(Color.Green), 10,10);
g.DrawString("Text on the Screen", red14Italic, new SolidBrush(Color.Red), 10,10);
FontFamily class provides members to get information about a family of fonts. These members include GetName, GetLineSpacing, GetEmHeight, IsStyleAvailable and so on. All of them are self-explanatory.
Working with Pens
We've seen earlier in this chapter that the Graphics class's draw members such as DrawLine, DrawRectangle, DrawArc and so on use pens to draw objects. A pen draws a line with specified width and style. The System.Drawing.Pen and System.Drawing.Pens classes represent pens in GDI+. Some of the System.Drawing.Pen class properties are defined in the following table.
Public Instance Properties |
Brush |
Attached brush with a pen. |
Color |
Color of a pen. |
Dash Style |
Dashed line style. |
DashCap |
Style at the beginning and at the end of dashed lines. |
DashedOffset |
Distance from the start of a line before a dashed pattern. |
CustomStartCap, CustomEndCap |
Custom cap style at the beginning and end of the line. |
DashPattern |
Dash pattern. |
DashStyle |
Dashed line style. |
StartCap, EndCap |
Starts and ends the cap style. |
PenTyle |
Style of lines of a pen. |
Transform |
Geometric transformation of a pen. |
Width |
Width of a pen |
The System.Drawing.Pens class represents pens of all the colors. The System.Drawing.Pens class is non inheritable. For example,
Pen redPn = new Pen(Color.Red, 14);
Pen redPen = Pens.Red;
g.DrawLine(redPen, 10, 40, 50, 60);
You can set the other properties:
pnGreen.Width = 4;
pnGreen.DashStyle = DashStyle.DashDot;
The DashStyle members and their description are given below -
Member |
Description |
Custom |
custom dash style. |
Dash |
A line consisting of dashes. |
DashDot |
A line of a repeating pattern of dash-dot. |
DashDotDot |
A line of a repeating pattern of dash-dot-dot. |
Dot |
A line consisting of dots. |
Solid |
A solid line. |