programming4us
programming4us
MULTIMEDIA

.NET Compact Framework : Drawing Text

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
9/27/2010 5:22:38 PM
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[1] 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] In a fixed-pitch font, the width of every character in the font is the same.

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
FeatureCommentSample Program
Simple text drawingCall the DrawString method to draw text in a control or formSimpleDrawString—shows simplest text drawing, which involves creation of a brush (for text color) and use of the form’s default font
Simplest font creationCreate a font using the FontFamily enumeration to select between a fixed-pitch, serif, or sans serif font without regard to font face nameGenericFonts—creates and draws with each of the three generic font families, in a range of styles (regular, bold, italic, strikeout, and underline)
Font enumerationEnumerated fonts using the InstalledFontCollection objectFontPicker—Compact Framework program that creates fonts of a specific face name
Rotate textTo rotate text, you draw using a rotated font; do this by filling a LOGFONT structure and calling the FromLogFont member of the Font classRotateText—shows how much fun rotated text can really be
ClearType fontsClearType 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 fieldDoes not explicitly create a ClearType font; the RotateTextLOGFONT structure sample is useful because it shows how to fill in a
Calculate size of graphical textOptimal positioning of text requires calculating the size of the bounding box of drawn text. This is accomplished using the MeasureString methodMeasureString—shows how to use results from the MeasureString method in drawing
Setting text alignmentBy 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 parameterTextAlign—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[2] 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.

[2] System.Drawing.Graphics.

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[3] in text drawing.

[3] Win32 programmers may recall that the BackgroundColor attribute in a device context allows a text-drawing operation to also affect the background pixels.

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.

Figure 1. Output of the SimpleDrawString Program


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.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us