ch16_Drawing Text.pdf

(154 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 16
Drawing Text
This chapter covers controlling the appearance of text, using classes in the System.Drawing
namespace and also by using P/Invoke to drill through to the underlying Win32 libraries for useful
text drawing features that are otherwise not available in the Compact Framework.
Author's Note: In this review chapter C#-Specific Text is highlighted in YELLOW.
Drawing with fonts in unmanaged code .............................................................. 23
Cleanup ............................................................................................................... 23
Sample: RotateText.......................................................................................................... 24
Placing Text ................................................................................................................................... 27
Text Size and the MeasureString method ................................................................... 28
Sample: MeasureString .................................................................................................... 28
Text Alignment..................................................................................................................30
Sample: TextAlign ............................................................................................................ 31
Word Wrap........................................................................................................................ 36
Sample: WordWrap .......................................................................................................... 37
Text Color ...................................................................................................................................... 39
Foreground and Background Text Colors ........................................................................ 40
Sample: TextColor ............................................................................................................ 40
Conclusion ..................................................................................................................................... 46
The focus of this chapter is on drawing text on the screen of a smart device using the .NET
Compact Framework. As we describe in chapter 16, Compact Framework Graphics , the Compact
Framework supports a subset of the graphical output features of the Desktop Framework. (For
details on the differences, see the boxed text titled "Comparing CF and DF Text Drawing.") In
general, though, the text drawing support is quite rich. You can do all of the more commonly
required text drawing operations such as selecting different fonts by name, in different sizes and
styles, and also control the color of drawn text. Some subtle effects are not available, but this is a
small price to pay for the small memory footprint occupied by the Compact Framework.
Chapter 16 – Drawing Text
Page 1
11/6/2003
Copyright © 2003 Paul Yao & David Durant
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Drawing Text
A graphical environment provides the programmer with a rich set of tools for creating a wide
range of graphical images. Such an environment makes it possible to mingle pictures with text, to
display text in a range of font sizes and styles, and to use an array of effects to convey subtle
messages to users. In such an environment, text itself is treated as a graphical object. Drawing
text in a graphical environment is paradoxically more complex than drawing text in a non-
graphical environment. In a character-oriented world such as in a console program, fixed pitch
fonts are used which place text in orderly rows and columns. In a graphical world, by contrast,
both fixed and variable pitch fonts float in a sea of pixels. The increase in complexity allows for
the free mixing of text and graphics. This freedom comes at a cost, namely the extra effort
required to tame the complexity of graphical text and to use it to enhance a program's graphical
Compact Framework Text Drawing Support
A Compact Framework program can draw text using any available font. Using TrueType
fonts, that text output can be scaled to any desired size from 8 point on up to 72 point and beyond.
That text can be drawn in any available color, although most programs are likely to use the
default system text color. [Comment 17.3]
By drilling through to the underlying Win32 libraries, a Compact Framework program can do
a few more things such as drawing rotated text, and accessing Clear Type fonts. Table 16-1
summarizes Compact Framework text drawing features, how to access these features, and this
chapter's sample programs which illustrate each feature. [Comment 17.4]
Table 16-1 Text Drawing Features and Sample Programs [Comment 17.5]
Feature
Comment
Sample Program
Call the DrawString 1 method to
draw text in a control or form.
Simple text drawing
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
Font enumeration involves getting a
list of available font face names
installed in the system. Compact
Framework does not support font
enumeration, so we rely on a Win32
DLL that does the font enumeration
work for us
FontPicker – CF program that
creates fonts of specific face name.
FontList – Win32 DLL that
enumerates available system fonts.
Rotate text
Compact Framework does not
natively support rotated fonts. For
that, you must call the Win32 font
creation functions.
RotateText – CF program that
uses P/Invoke to call Win32 font
creation program.
ClearType fonts
ClearType is a font technology that
aids in reading small text on LCD
displays. Compact Framework does
not support this, so you must instead
call Win32 font creation functions as
1 The fully-qualified name is System.Drawing.Graphics .
Chapter 16 – Drawing Text
Page 2
11/6/2003
Copyright © 2003 Paul Yao & David Durant
870584807.034.png 870584807.035.png 870584807.036.png 870584807.001.png 870584807.002.png 870584807.003.png 870584807.004.png 870584807.005.png 870584807.006.png 870584807.007.png 870584807.008.png 870584807.009.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Feature
Comment
Sample Program
illustrated in the RotateText sample.
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 MeasureString
method in drawing.
Setting text alignment
By default, text is aligned to the
upper-left corner of the text box.
Compact Framework does support
alternative text alignment settings, so
different alignments require
modifying the (x,y) location where
you draw text.
TextAlign – Shows twelve ways
to align text by mixing and matching
four vertical alignments and three
horizontal alignments
Word wrap
Compact Framework supports two
versions of the DrawString method,
but only one of them supports word
wrap. The version which uses a
rectangle supports word wrap. In
addition to wrapping the text to the
indicated rectangle, the text is clipped
to that rectangle as well – that is, text
is only drawn within the specified
rectangle.
WordWrap – shows use of the
version of the DrawString method
which supports wrapping and
clipping a text string to a specified
output rectangle.
Text color
Set text foreground color by creating
a brush. Set text background color by
drawing a colored rectangle before
drawing the text.
TextColors – Shows three ways
to pick colors: (1) using named
colors, (2) using system colors, (3)
with the color picker dialog box
(requires P/Invoke)
The DrawString Function
All Compact Framework text drawing is done with the DrawString method, a member of
the Graphics class 2 , available in two overloaded implementations. We start our discussion with
the simpler of the two overloaded functions, which accepts a pair of (x,y) values for the location
where the text is to be drawn. This method does not provide automatic word-wrapping support.
So if a string is too long for the available drawing space, the "extra" characters disappear. A
second version of DrawString , which we discuss later in this chapter, does word wrapping for
The simpler version of DrawString , which takes five parameters, is defined as follows:
public void DrawString(
string str,
Font font,
Brush brText,
float x,
float y);
This first parameter, str , identifies the string to draw. While automatic word-wrap is not
supported, a carriage-return or linefeed character within the string causes the string to display in
2 The fully qualified name is System.Drawing.Graphics .
Chapter 16 – Drawing Text
Page 3
11/6/2003
Copyright © 2003 Paul Yao & David Durant
870584807.010.png 870584807.011.png 870584807.012.png 870584807.013.png 870584807.014.png 870584807.015.png 870584807.016.png 870584807.017.png 870584807.018.png 870584807.019.png 870584807.020.png 870584807.021.png 870584807.022.png 870584807.023.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
multiple lines. (In C#, insert a new line using the \n or \r character; in VB, a new line is caused
by including any of the following constants in a string: vbLf , vbCr , or vbCrLf .) [Comment 17.9]
The second parameter, font , is the font used for drawing the characters. This could be the
default font (the Font property) of a control, or a font which you dynamically create. [Comment
The third parameter, brText , identifies the brush which specifies the color of the text
foreground pixels; in the world of .NET programming, the background pixels are always left
untouched 3 when you draw text. [Comment 17.11]
The fourth and fifth parameters, x and y , indicate the text drawing location. This location is
the upper-left corner of the rectangle which bounds the text. These coordinate values are single-
precision floating point values, which is different from the integer coordinates used to draw raster
and vector graphics. [Comment 17.12]
Sample: SimpleDrawString
Our first sample program shows the simplest way to draw in a form. Figure 16-1 shows the
program's output. This program uses the form's default font to draw a character string using the
system default window text color. [Comment 17.13]
We highlight the text placement location with a pair of lines, to make it obvious that the (x,y)
location identifies the upper-left corner of an imaginary box that bounds the text string. The
Compact Framework does not have any built-in facility to request alternative text alignments,
which you might use to center a string over a column of data or center the text inside a graphical
image. The TextAlign sample, shown later in this chapter, shows some simple techniques for
manually creating eleven alternative text alignments to the default upper-left alignment.
Text Drawing Performance
The SimpleDrawString sample shows one way to draw text, but this is
not necessarily the fastest way to draw text. We notice a 10%
improvement in drawing speed when we create a font ahead of time,
instead of using the Font property of the control. For most purposes, the
Font property works well for drawing text. To squeeze the fastest text
drawing from the Compact Framework, however, we suggest you cache
the font and passing that font to the DrawString function. [Comment 17.15]
3 Win32 programmers may recall that the Background Color attribute in a DC allows a text
drawing operation to also affect the background pixels.
Chapter 16 – Drawing Text
Page 4
11/6/2003
Copyright © 2003 Paul Yao & David Durant
870584807.024.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Fig ure 16-1 Output from SimpleDrawStr ing sample [Comment 17.16]
Listing 16-1 Fragment from SimpleDrawString.cs showing Paint event handler
private float xDraw = 10;
private float yDraw = 10;
private void
FormMain_Paint(object sender, PaintEventArgs e)
{
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);
// Cleanup
brText.Dispose();
penBlack.Dispose();
}
Font Selection
We now turn our attention to the subject of fonts. Font selection provides the primary way to
control the appearance of text. Windows CE supports two basic font technologies: bitmap fonts
Chapter 16 – Drawing Text
Page 5
11/6/2003
Copyright © 2003 Paul Yao & David Durant
870584807.025.png 870584807.026.png 870584807.027.png 870584807.028.png 870584807.029.png 870584807.030.png 870584807.031.png 870584807.032.png 870584807.033.png
 
Zgłoś jeśli naruszono regulamin