Condividi tramite


Visual Basic Concepts

Working with Fonts

Text is displayed using a font — a set of characters of the same typeface, available in a particular size, style, and weight.

The Windows 95, Windows 98, and Windows NT operating systems provide you and your users with a complete set of standard fonts. TrueType® fonts are scaleable, which means they can reproduce a character at any size. When you select a TrueType font, it is rendered into the selected point size and displayed as a bitmap on the screen.

When printing, the selected TrueType font or fonts are rendered into the appropriate size and then sent to the printer. Therefore, there is no need for separate screen and printer fonts. Printer fonts will be substituted for TrueType fonts, however, if an equivalent font is available, which increases print speed.

Choosing Fonts for Your Application

Remember that a user of your application may not have the fonts you used to create the application. If you select a TrueType font that a user doesn’t have, Windows selects the closest matching font on the user’s system. Depending on the design of your application, this may cause problems for the user. For example, the font Windows selects may enlarge text so that labels overlap on the screen.

One way to avoid font problems is to distribute the necessary fonts with your application. (You will probably need to obtain permission from the copyright holder of the font to distribute it with your application.)

You can also program your application to check among the fonts available in the operating system for the fonts you use. If the font doesn’t reside in the operating system, you can program the application to choose a different font from the list.

Another way to avoid font problems is to use fonts users are most likely to have on their systems. If you use fonts from a specific version of Windows, you may have to specify that version as a system requirement of your application.

Checking Available Fonts

Your program can easily determine whether matching fonts are available on both the user’s system and printer. The Fonts property applies to the Printer and Screen objects. An array returned by the Fonts property is a list of all of the fonts available to a printer or screen. You can iterate through the property array, and then search for matching name strings. This code example determines whether the system has a printer font that matches the font of the selected form:

Private Sub Form_Click ()
Dim I As Integer, Flag As Boolean
   For I = 0 To Printer.FontCount - 1
      Flag = StrComp (Font.Name,Printer.Fonts(I), 1)
      If Flag = True Then
         Debug.Print "There is a matching font."
         Exit For
      End If
   Next I
End Sub

For More Information   For information about setting font properties, see "Setting Font Characteristics" later in this chapter. For information about fonts in East Asian systems, see "Font, Display, and Print Considerations in a DBCS Environment" in "International Issues."

Creating Your Own Font Types

If you set a reference to Standard OLE Types using the References dialog box, you can use the StdFont class to create your own font types. If you view the Object Browser, you will notice that there are StdFont and Font classes. The Font class is derived from the StdFont base class and is supported by all controls.

You can use the following syntax:

Dim MyFont As Font

But, you cannot use:

Dim MyFont As New Font

Instead, to create your own font or picture types, use code like the following:

Dim MyFont As New StdFont
With MyFont
   .Bold = True
   .Name = "Arial"
End With
Set Text1.Font = MyFont