A
graphical environment gives programmers a rich set of tools for
creating a wide range of graphical effects. You can freely mix pictures
with text, display text in different sizes and styles, and use various
effects to convey subtle—and sometimes not-so-subtle—messages to your
users. Text itself is a graphical object.
Drawing
text in a graphical environment is paradoxically more complex than
drawing text in a nongraphical environment. In a character-oriented
world such as a console program, fixed-pitch fonts
place text in orderly rows and columns. In a graphical world, by
contrast, fixed-pitch fonts and variable-pitch fonts float in a sea of
pixels. Drawing text in a graphical environment takes a little more
effort, but the results are well worth the time and energy you put into
it.
1. Text-Drawing Support in the .NET Compact Framework
A
.NET Compact Framework program can draw text using a built-in font, or
you can add new fonts by dropping font (*.ttf) files into the \windows\fonts
directory. The use of TrueType fonts allows text to be scaled to any
size from 8 points up to 72 points (and beyond). That text can be drawn
in any available color, although most programs use black. And you can
even rotate that text. Table 1
summarizes Compact Framework text-drawing features, how to access these
features, and this chapter’s sample programs which illustrate each
feature.
Table 1. Text-Drawing Features and Sample Programs
Feature | Comment | Sample Program |
---|
Simple text drawing | Call the DrawString method to draw text in a control or form | SimpleDrawString—shows simplest text drawing, which involves creation of a brush (for text color) and use of the form’s default font |
Simplest font creation | Create a font using the FontFamily enumeration to select between a fixed-pitch, serif, or sans serif font without regard to font face name | GenericFonts—creates
and draws with each of the three generic font families, in a range of
styles (regular, bold, italic, strikeout, and underline) |
Font enumeration | Enumerated fonts using the InstalledFontCollection object | FontPicker—Compact Framework program that creates fonts of a specific face name |
Rotate text | To rotate text, you draw using a rotated font; do this by filling a LOGFONT structure and calling the FromLogFont member of the Font class | RotateText—shows how much fun rotated text can really be |
ClearType fonts | ClearType is a font technology that makes text more legible on LCD displays. Create ClearType fonts by filling in a LOGFONT structure and specifying CLEARTYPE_QUALITY in the lfQuality field | Does not explicitly create a ClearType font; the RotateTextLOGFONT structure sample is useful because it shows how to fill in a |
Calculate size of graphical text | Optimal positioning of text requires calculating the size of the bounding box of drawn text. This is accomplished using the MeasureString method | MeasureString—shows how to use results from the MeasureString method in drawing |
Setting text alignment | By
default, text is aligned to the upper-left corner of the text box.
Override this default by setting alignment properties in the StringFormat structure and calling the DrawString overrides which accept a StringFormat as a parameter | TextAlign—shows nine ways to align text by mixing and matching three vertical alignments and three horizontal alignments |
2. The DrawString Method
All Compact Framework text drawing is done with the DrawString method, a member of the Graphics class with four available overloaded implementations. We start our discussion with the simplest
of the overloaded functions, which accepts a pair of (x,y)
single-precision floating-point values for the text location.
The simplest version of the DrawString method, with five parameters, is defined as follows:
public void DrawString(
string str,
Font font,
Brush brText,
Single x,
Single y);
The first parameter, str,
identifies the string to draw. While automatic word wrap is not
supported, a carriage return within a string draws multiple lines. (In
C#, insert a new line with the \n character.)
The second parameter, font, is the font used for drawing the characters. This could be the default font of a control (the Font property) or a font that you create.
The third parameter, brText, identifies the brush for drawing text foreground pixels; in .NET programming, background pixels are always untouched in text drawing.
The fourth and fifth parameters, x and y,
indicate the text-drawing location. This location is the upper-left
corner of the rectangle that bounds the text. These coordinates are
single-precision floating-point values, which is different from the
integer coordinates used to draw raster and vector graphics.
3. A Sample Program: SimpleDrawString
Our first sample program shows the simplest way to draw in a form. Figure 1
shows the program’s output. This program uses the form’s default font
to draw a character string using the system’s default window text color.
Listing 1 shows the Paint event handler method for our simple text-drawing sample. This method is called whenever the window’s client area needs drawing.
Listing 1. Fragment from SimpleDrawString.cs Showing the Paint Event Handler
private void formMain_Paint(object sender, PaintEventArgs e) { Single xDraw = 10; Single yDraw = 10;
Brush brText = new SolidBrush(SystemColors.WindowText); e.Graphics.DrawString("Simple Draw String", Font, brText, xDraw, yDraw);
// Highlight origin. int x = (int)xDraw; int y = (int)yDraw; Pen penBlack = new Pen(Color.Black); e.Graphics.DrawLine(penBlack, x, y, x-8, y); e.Graphics.DrawLine(penBlack, x, y, x, y-8); }
|
In this sample, the text is drawn at (10,10), the coordinates assigned to the xDraw and yDraw variables. The second parameter specifies the font to use; in this code, we use the Font property from the formMain form. In later examples, we show several ways to provide total control over the appearance of text.
Our program calls the DrawLine
method to draw two lines—one horizontal and one vertical—that intersect
at (10,10). The intersection of those two lines shows the default text
alignment, at the upper-left corner of the drawn text.
The
.NET Compact Framework supports other alignments, as we show later in
this chapter. This capability lets you do things such as centering a
string over a column of data or centering a text label within a
graphical image. This chapter’s TextAlign sample shows all nine possible alignments.