home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Creating Fonts

The Windows API function CreateFont creates a new font that a program can use in a form or picture box. CreateFont is declared with the following code:

Declare Function CreateFont Lib “gdi32” Alias “CreateFontA” ( _
    ByVal Height As Long, ByVal Width As Long, _
    ByVal Escapement As Long, ByVal Orientation As Long, _
    ByVal Weight As Long, ByVal Italic As Long, _
    ByVal Underline As Long, ByVal StrikeOut As Long, _
    ByVal CharSet As Long, ByVal OutputPrecision As Long, _
    ByVal ClipPrecision As Long, ByVal Quality As Long, _
    ByVal PitchAndFamily As Long, ByVal Face As String) _
    As Long

The CreateFont function returns a handle to a logical font that describes the font requested by the CreateFont function. The font mapper then uses this description to select a physical font that matches the description as closely as possible on the computer. The match may not be very close if the fonts available on the computer cannot provide the requested features. Many of the parameters passed to CreateFont determine how the font mapper acts if it cannot exactly match the description.

Generally, a program will specify the Face parameter to indicate the font name that should be used and the Height parameter to indicate the font’s size. Many of the other parameters take on default values if they are set to the value zero.

CreateFont’s parameters are described briefly in the following list. For more information, search for CreateFont in the online help file WIN31WH.HLP.

•  Height. Gives the height for the font in logical units.
•   Width. Gives the average width of characters. If zero, the font mapper selects a Width that matches the Height.
•  Escapement. The angle of the text’s slope in tenths of degrees, measured counterclockwise from horizontal. To create vertical text, AppointmentBook uses an escapement of 900 (90 degrees in tenths of degrees).
•  Orientation. Gives the orientation of the characters in tenths of degrees. Windows assumes this is the same as Escapement so it ignores Orientation.
•  Weight. Gives the font’s weight. For a default font weight, the program should specify 0. For other weights it should specify a multiple of 100 up to 900, 100 indicating a thin weight and 900 indicating a very heavy weight. The most commonly used values are 400 for a normal font and 700 for bold.
•  Italic. If not zero, indicates an italic font.
•  Underline. If not zero, indicates an underlined font.
•  StrikeOut. If not zero, indicates a font with strikeout.
•  CharSet. Specifies the font’s character set. Usually a program should set this parameter to 0 to indicate an ANSI character set.
•  OutputPrecision. Specifies how closely the font must match the height, width, escapement, character orientation, and pitch parameters. The program should specify 0 for the font’s default precision. It can also specify a value between 1 and 8 where smaller values generally indicate less precision.
•  ClipPrecision. Indicates how characters that are partially visible should be clipped. If the program is using a font with nonzero escapement, it should add the value CLIP_LH_ANGLES (16) to the precision. The default precision is 0.
•  Quality. Indicates how closely the physical font must match the logical font. This parameter can have one of three values:
0.  Default. The font’s quality is not too important.
1.  Draft. The font mapper will scale a raster font if necessary to get the size requested. This may produce jagged results if the closest available size is not too close to the size specified.
2.  Proof. The font’s quality is very important. The font mapper will not scale raster fonts. Instead, it will select a font that matches the size specified even if it does not exactly match the other parameters.
•  PitchAndFamily. Specifies the pitch and family of the font, combined using the Or operator. This determines what kind of font is used if the font mapper cannot find a very good match.
Pitch. Tells whether the font’s characters all have the same cell size. The value 0 indicates the default pitch for the font. The value 1 indicates a fixed pitch font like this one. The value 2 indicates a variable pitch font like this one.
Family. Tells generally what sort of appearance the font should have. Table 3.1 lists values the program can use.
•  Face. The font face name such as “Times New Roman” or “Courier New.”

Windows 3.1 comes with the Arial, Courier New, and Times New Roman fonts. Each of these comes in bold, italic, and bold italic varieties. Even though there is no guarantee that these fonts will be available on all computers, they are more likely to be available than many other fonts. If a program uses them, it increases the chances that it will run correctly on a wide variety of computers.

Table 3.1 Font Family Values
Value Meaning Examples
0 Default. (Depends on the font.)
16 Roman. Variable width strokes with serifs. Times New Roman
32 Swiss. Variable width strokes, no serifs. FrankfurtGothic
48 Modern. Constant width strokes, with or without serifs. Courier New
64 Script. Designed to look like handwriting.
80 Novelty fonts.

Using CreateFont

After the program obtains a logical font handle using CreateFont, it can use the SelectObject API function to make a form or picture box use the font. SelectObject returns the handle of the previously selected font. When the program is done using the new font, it should reselect the original font with SelectObject and then delete the new font with the DeleteObject function. This frees system resources that are held by the font.

The VerticalText subroutine in AppointmentBook’s module CalVC.CLS creates a font rotated by 90 degrees. It takes as parameters the text to be displayed, the X coordinate that should mark the left edge of the text, and two Y coordinate values between which the text should be centered. It also takes parameters indicating the font’s height and weight, plus values the subroutine should pass to CreateFont indicating whether the font should have italic, underlined, or strike out style. The final parameter gives the font’s face name.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.