Font Control

This section introduces methods and constants that are related to font control. The properties of Font objects cannot be modified. If you need a different Font, you must create a new Font object. There are many overloaded versions of the Font constructor for initializing Font objects. Some properties of class Font are summarized in Fig. 17.8.

Figure 17.8. Font class read-only properties.

Property

Description

Bold

Returns true if the font is bold.

FontFamily

Returns the Font's FontFamilya grouping structure to organize fonts and define their similar properties.

Height

Returns the height of the font.

Italic

Returns TRue if the font is italic.

Name

Returns the font's name as a string.

Size

Returns a float value indicating the current font size measured in design units (design units are any specified unit of measurement for the font).

SizeInPoints

Returns a float value indicating the current font size measured in points.

Strikeout

Returns true if the font is in strikeout format.

Underline

Returns true if the font is underlined.

Note that the Size property returns the font size as measured in design units, whereas SizeInPoints returns the font size as measured in points (the more common measurement). Design units allow the font size to be specified in one of several units of measurement, such as inches or millimeters. Some versions of the Font constructor accept a GraphicsUnit argument. GraphicsUnit is an enumeration that allows you to specify the unit of measurement that describes the font size. Members of the GraphicsUnit enumeration include Point (1/72 inch), Display (1/75 inch), Document (1/300 inch), Millimeter, Inch and Pixel. If this argument is provided, the Size property contains the size of the font as measured in the specified design unit, and the SizeInPoints property contains the corresponding size of the font in points. For example, if we create a Font having size 1 and specify the unit of measurement as GraphicsUnit.Inch, the Size property will be 1 and the SizeInPoints property will be 72, because there are 72 points in an inch.The default measurement for the font size is GraphicsUnit.Point (thus, the Size and SizeInPoints properties will be equal).

Class Font has several constructors. Most require a font name, which is a string representing a font currently supported by the system. Common fonts include Microsoft SansSerif and Serif. Most Font constructors also require as arguments the font size and font style. The font style is specified with a constant from the FontStyle enumeration (Bold, Italic, Regular, Strikeout and Underline, or a combination of these). You can combine font styles with the | operator, as in FontStyle.Italic | FontStyle.Bold, which makes a font both italic and bold. Graphics method DrawString sets the current drawing fontthe font in which the text displaysto its Font argument.

Common Programming Error 17 1

Specifying a font that is not available on a system is a logic error. If this occurs, C# will substitute that system's default font.

 

Drawing Strings in Different Fonts

The program in Fig. 17.9 displays text in different fonts and sizes. The program uses the Font constructor to initialize the Font objects (lines 24, 28, 32 and 36). Each call to the Font constructor passes a font name (e.g., Arial, Times New Roman, Courier New or Tahoma) as a string, a font size (a float) and a FontStyle object (style). Graphics method DrawString sets the font and draws the text at the specified location. Note that line 20 creates a DarkBlue SolidBrush object (brush). All strings drawn with that brush appear in DarkBlue.

Figure 17.9. Fonts and FontStyles.

 1 // Fig. 17.9 UsingFonts.cs
 2 // Fonts and FontStyles.
 3 using System;
 4 using System.Drawing;
 5 using System.Windows.Forms;
 6
 7 // demonstrate font constructors and properties
 8 public partial class UsingFonts : Form
 9 {
10 // default constructor
11 public UsingFonts()
12 {
13 InitializeComponent();
14 } // end constructor
15
16 // demonstrate various font and style settings 17 protected override void OnPaint( PaintEventArgs paintEvent ) 18 { 19 Graphics graphicsObject = paintEvent.Graphics; 20 SolidBrush brush = new SolidBrush( Color.DarkBlue ); 21 22 // arial, 12 pt bold 23 FontStyle style = FontStyle.Bold; 24 Font arial = new Font( "Arial" , 12, style ); 25 26 // times new roman, 12 pt regular 27 style = FontStyle.Regular; 28 Font timesNewRoman = new Font( "Times New Roman", 12, style ); 29 30 // courier new, 16 pt bold and italic 31 style = FontStyle.Bold | FontStyle.Italic; 32 Font courierNew = new Font( "Courier New", 16, style ); 33 34 // tahoma, 18 pt strikeout 35 style = FontStyle.Strikeout; 36 Font tahoma = new Font( "Tahoma", 18, style ); 37 38 graphicsObject.DrawString( arial.Name + 39 " 12 point bold.", arial, brush, 10, 10 ); 40 41 graphicsObject.DrawString( timesNewRoman.Name + 42 " 12 point plain.", timesNewRoman, brush, 10, 30 ); 43 44 graphicsObject.DrawString( courierNew.Name + 45 " 16 point bold and italic.", courierNew, 46 brush, 10, 54 ); 47 48 graphicsObject.DrawString( tahoma.Name + 49 " 18 point strikeout.", tahoma, brush, 10, 75 ); 50 } // end method OnPaint 51 } // end class UsingFonts

Font Metrics

You can determine precise information about a font's metrics (or properties), such as height, descent (the amount that characters dip below the baseline), ascent (the amount that characters rise above the baseline) and leading (the difference between the ascent of one line and the decent of the previous line). Figure 17.10 illustrates these font metrics.

Figure 17.10. Font metrics illustration.

Class FontFamily defines characteristics common to a group of related fonts. Class FontFamily provides several methods used to determine the font metrics that are shared by members of a particular family. These methods are summarized in Fig. 17.11.

Figure 17.11. FontFamily methods that return font-metric information.

Method

Description

GetCellAscent

Returns an int representing the ascent of a font as measured in design units.

GetCellDescent

Returns an int representing the descent of a font as measured in design units.

GetEmHeight

Returns an int representing the height of a font as measured in design units.

GetLineSpacing

Returns an int representing the distance between two consecutive lines of text as measured in design units.

The program in Fig. 17.12 displays the metrics of two fonts. Line 23 creates Font object arial and sets it to 12-point Arial font. Line 24 uses Font property FontFamily to obtain object arial's FontFamily object. Lines 2728 output the string representation of the font. Lines 3044 then use methods of class FontFamily to obtain the ascent, descent, height and leading of the font and draw strings containing that information. Lines 4768 repeat this process for font sansSerif, a Font object derived from the MS Sans Serif FontFamily.

Figure 17.12. FontFamily class used to obtain font-metric information.

 1 // Fig 17.12: UsingFontMetrics.cs
 2 // Displaying font metric information
 3 using System;
 4 using System.Drawing;
 5 using System.Windows.Forms;
 6
 7 // display font information
 8 public partial class UsingFontMetrics : Form
 9 {
10 // default constructor 11 public UsingFontMetrics() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // displays font information 17 protected override void OnPaint( PaintEventArgs paintEvent ) 18 { 19 Graphics graphicsObject = paintEvent.Graphics; 20 SolidBrush brush = new SolidBrush( Color.DarkBlue ); 21 22 // Arial font metrics 23 Font arial = new Font( "Arial", 12 ); 24 FontFamily family = arial.FontFamily; 25 26 // display Arial font metrics 27 graphicsObject.DrawString( "Current Font: " + 28 arial, arial, brush, 10, 10 ); 29 30 graphicsObject.DrawString( "Ascent: " + 31 family.GetCellAscent( FontStyle.Regular ), arial, 32 brush, 10, 30 ); 33 34 graphicsObject.DrawString( "Descent: " + 35 family.GetCellDescent( FontStyle.Regular ), arial, 36 brush, 10, 50 ); 37 38 graphicsObject.DrawString( "Height: " + 39 family.GetEmHeight( FontStyle.Regular ), arial, 40 brush, 10, 70 ); 41 42 graphicsObject.DrawString( "Leading: " + 43 family.GetLineSpacing( FontStyle.Regular ), arial, 44 brush, 10, 90 ); 45 46 // display Sans Serif font metrics 47 Font sanSerif = new Font( "Microsoft Sans Serif", 48 14, FontStyle.Italic ); 49 family = sanSerif.FontFamily; 50 51 graphicsObject.DrawString( "Current Font: " + 52 sanSerif, sanSerif, brush, 10, 130 ); 53 54 graphicsObject.DrawString( "Ascent: " + 55 family.GetCellAscent( FontStyle.Regular ), sanSerif, 56 brush, 10, 150 ); 57 58 graphicsObject.DrawString( "Descent: " + 59 family.GetCellDescent( FontStyle.Regular ), sanSerif, 60 brush, 10, 170 ); 61 62 graphicsObject.DrawString( "Height: " + 63 family.GetEmHeight( FontStyle.Regular ), sanSerif, 64 brush, 10, 190 ); 65 66 graphicsObject.DrawString( "Leading: " + 67 family.GetLineSpacing( FontStyle.Regular ), sanSerif, 68 brush, 10, 210 ); 69 } // end method OnPaint 70 } // end class UsingFontMetrics

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

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