Drawing Text Using the Font Class


The next example uses the Font class to draw text. the Font class encapsulates the three main characteristics of a font, which are the font family, the font size, and the font style. the Font class is in the System.Drawing namespace.

According to the .NET documentation, a font family "abstracts a group of type faces having a similar basic design." This is a fancy way of saying that font families are things like Courier, Arial, or Times New Roman.

The font Size property represents the size of the font type. However, in the .NET Framework, this size is not strictly the point size. It can be the point size, but you can change a property called the GraphicsUnit via the Unit property, which defines the unit of measure for the font. To refresh your memory, one point is equal to 1/72 of an inch, so a 10-point font is 10/72 of an inch high. Based on the GraphicsUnit enumeration, you can specify the unit for the font as one of the following:

  • point (1/72 of an inch)

  • display (1/75 of an inch)

  • document (1/300 of an inch)

  • inch

  • millimeter

  • pixel

This means that you have an unprecedented flexibility in specifying the desired size of your font. One possible use for this might be if you are writing a text drawing routine that needs to work in an acceptable way on very high-resolution displays, low-resolution displays, and printers.

When drawing text, given a specific font, and given a specific drawing surface, you often need to know the width in pixels of a specified string of text. It is pretty clear why different fonts will have an effect on the width in pixels of a string — a smaller font will result in a width of fewer pixels. However, it is equally as important to know the drawing surface, because the pixel resolutions of different drawing surfaces are different. Typically, the screen has 72 pixels per inch. Printers can be 300 pixels per inch, 600 pixels per inch, and sometimes even more. Use the MeasureString() method of the Graphics object to calculate the width of a string for a given font.

The font Style property refers to whether the type is italicized, emboldened, struck-through, or underlined.

Important

Always call Dispose( ) on Font objects.

It is important to call Dispose() on Font objects that you create, or use the using block: otherwise, your application may deplete Windows resources.

When drawing text, you use a Rectangle to specify the bounding coordinates of the text to be drawn. Typically, the height of this rectangle should be the height of the font or a multiple of the height of the font. This would only be different when drawing some special effect using clipped text.

The StringFormat class encapsulates text layout information, including alignment and line spacing. The following example shows right and centered text justification using the StringFormat class. In the following Try It Out, you will create a Font object, and draw some text with it.

Try It Out – Font Example

image from book

Follow these steps to practice drawing text in a variety of ways:

  1. Create a new Windows application called DrawText in the directory C:\BegVCSharp\Chapter30.

  2. In the constructor of the Form1 class, add a call to SetStyle() after the call to InitializeComponent(). You also change the bounds of the window to give enough room to display the text that you want to display. The modified constructor is as follows:

    public Form1() {    InitializeComponent(); SetStyle(ControlStyles.Opaque, true); Bounds = new Rectangle(0, 0, 500, 300); }
  3. Now, add an OnPaint() method to your class:

     protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; int y = 0; g.FillRectangle(Brushes.White, ClientRectangle); // Draw left-justified text. Rectangle rect = new Rectangle(0, y, 400, Font.Height); g.DrawRectangle(Pens.Blue, rect); g.DrawString("This text is left justified.", Font, Brushes.Black, rect); y += Font.Height + 20; // Draw right-justified text. Font aFont = new Font("Arial", 16, FontStyle.Bold | FontStyle.Italic); rect = new Rectangle(0, y, 400, aFont.Height); g.DrawRectangle(Pens.Blue, rect); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far; g.DrawString("This text is right justified.", aFont, Brushes.Blue, rect, sf); y += aFont.Height + 20; // Manually call Dispose(). aFont.Dispose(); // Draw centered text. Font cFont = new Font("Courier New", 12, FontStyle.Underline); rect = new Rectangle(0, y, 400, cFont.Height); g.DrawRectangle(Pens.Blue, rect); sf = new StringFormat(); sf.Alignment = StringAlignment.Center; g.DrawString("This text is centered  and underlined.", cFont, Brushes.Red, rect, sf); y += cFont.Height + 20; // Manually call Dispose(). cFont.Dispose(); // Draw multiline text. Font trFont = new Font("Times New Roman", 12); rect = new Rectangle(0, y, 400, trFont.Height * 3); g.DrawRectangle(Pens.Blue, rect); String longString = "This text is much longer, and drawn "; longString += "into a rectangle that is higher than "; longString += "one line, so that it will wrap.  It is "; longString += "very easy to wrap text using GDI+."; g.DrawString(longString, trFont, Brushes.Black, rect); // Manually call Dispose(). trFont.Dispose(); } 

  4. When you compile and run the code, it will create the window shown in Figure 30-11.

    image from book
    Figure 30-11

How It Works

This example contains a few of the most common text drawing operations.

As usual, you assign a reference to the Graphics object to a local variable, for your convenience. You also paint the background of the window white.

When drawing the text, you calculate the bounding rectangle for your text. You get the height of the font using the Height property. For illustrative purposes, you draw this rectangle in blue so that the bounding rectangle of your text is very clear. When you draw the text, you pass the text, the font, a brush, and a bounding rectangle:

// Draw left-justified text. Rectangle rect = new Rectangle(0, y, 400, Font.Height); g.DrawRectangle(Pens.Blue, rect); g.DrawString("This text is left justified.", Font,              Brushes.Black, rect); 

You specify only the rectangle in which the text will go. The baseline of a font is the imaginary line that most of the characters of the font "sit" on. GDI+ and the font itself determine where the actual baseline will go — you have no control over that.

When you draw the text, you pass a brush to the DrawString() function. In this example, you pass only brushes that have a solid color. You could just as easily have passed other types of brushes, such as a gradient brush. After you have introduced images in the next section, I will demonstrate drawing text using a TextureBrush.

The first time that you draw text in this example, you use the default font for the form. This font is referenced in the Font property, which is inherited from Control.

g.DrawString("This is a left justified string.", Font,               Brushes.Black, rect);

The next time you draw text in this example, you create a new instance of a Font:

Font aFont = new Font("Arial", 16, FontStyle.Bold | FontStyle.Italic);

This example shows not only how to create a new instance of a font, but how to give it a style. In this case, the style is bold and italic.

The example also shows creating a StringFormat object, so that you can draw right-justified and centered text. In GDI+, right-justified text alignment is referred to as Far alignment. Left-justified text is Near alignment.

StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far;

Finally, you draw some multiline text. Using GDI+, it could not be easier. All you need to do is to specify a rectangle where the width is less than the length of the string (in pixels), and the height is sufficient to draw multiple lines. In this case, you made the height equal to three times the font height.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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