Using Stock Fonts

When you call TextOut, TabbedTextOut, ExtTextOut, or DrawText to write text, Windows uses the font currently selected in the device context. The font defines a particular typeface and size. The easiest way to write text in a choice of fonts is to use the six stock fonts that Windows provides. You can first obtain a handle to a stock font by calling:

hFont = GetStockObject (nFont) ;

where nFont is one of the six identifiers discussed below. You can then select that font into the device context:

SelectObject (hdc, hFont) ;

Or you can do it in one step:

SelectObject (hdc, GetStockObject (nFont)) ;

GetStockObject is the same function that we used in Chapter 12 to obtain stock pens and brushes; SelectObject we used in Chapters 12 and 13 to select pens, brushes, bitmaps, and regions into the device context.

The font selected in the default device context is called the system font and is identified by the GetStockObject parameter SYSTEM_FONT. This is the proportional ANSI character set font that Windows uses for text in menus, dialog boxes, message boxes, and window caption bars. Specifying SYSTEM_FIXED_FONT in GetStockObject gives you a handle to a fixed-pitch ANSI font compatible with the system font used in versions of Windows prior to version 3. We've frequently encountered this font in sample programs in this book when using a fixed-pitch font seemed to be easier than using a proportional font. The OEM_FIXED_FONT identifier gives you a handle to a font that is often called the terminal font. This is the font that Windows uses for windowed DOS character-mode programs. On most devices, the terminal font is similar to the fixed-pitch system font but uses the OEM rather than the ANSI character set. (The ANSI and OEM character sets are discussed in Chapter 4.)

The identifier ANSI_FIXED_FONT gives you a handle to a Courier font that is usually smaller than the system or terminal font. You can obtain a handle to a font with variable character widths by using the identifier ANSI_VAR_FONT. This returns a handle to a Helvetica or Times Roman font, either of which is usually smaller than the system font.

Finally, the identifier DEVICE_DEFAULT_FONT is designed to return a handle to a font that is built into the output device and that is most suitable for the device. For most graphics-based video displays, no font meets this condition, so the identifier returns a handle to the system font. For a dot-matrix printer, however, this identifier returns a handle to a font that is specific to the printer and that in some cases does not require Windows to operate the printer in a graphics mode.

When you select a new font into a device context, you must calculate the font's character height and average character width using GetTextMetrics. If you've selected a proportional font, be aware that the average character width is really an average and that a given character can have a smaller or larger width. Later in this chapter you'll learn how to use GetTextExtent to calculate the full width of a string made up of variable-width characters.

Although GetStockObject certainly offers the easiest access to different fonts, you don't have much control over what font Windows gives you. You'll see shortly how you can be very specific about the typeface and type size that you want.