Using Stock Fonts

You are not limited to using the system font in your application. GDI offers a variety of stock fonts that you can retrieve and use as desired. To use stock fonts in your application, you must specify the type of font you want in the GetStockObject function. GetStockObject creates the font you request and returns a handle to the font that you can use to select into a device context. GDI offers the following stock fonts:

ANSI_FIXED_FONT

Specifies a fixed-pitch font based on the ANSI character set. For example, a Courier font is typically used, if one is available.

ANSI_VAR_FONT

Specifies a variable-width font based on the ANSI character set. For example, a Helv font is typically used, if it is available.

DEVICEDEFAULT_FONT

Specifies a font preferred by the given device. This font depends on how the GDI font mapper interprets font requests, so the font may vary widely from device to device.

OEM_FIXED_FONT

Specifies a fixed-pitch font based on an OEM character set. OEM character sets vary from system to system. For IBM computers and compatibles, the OEM font is based on the IBM PC character set.

SYSTEM_FONT

Specifies the system font. This is a variable-pitch font based on the ANSI character set, and is used by the system to display window captions, menu names, and text in dialog boxes. The system font is always available. Other fonts are available only if they have been installed.

To use a stock font, create it by using the GetStockObject function, then select the font handle into the device context by using the SelectObject function. Any subsequent calls to TextOut will use the selected font. The following example shows how to use the variable-width ANSI font:

HFONT hFont;

HFONT hOldFont;

.

.

.

hFont = GetStockObject(ANSI_VAR_FONT);

if (hOldFont = SelectObject(hDC, hFont)) {

TextOut(hDC, 10, 10, “This is a sample string”, 23);

SelectObject(hDC, hOldFont);

}

As you would with any other GDI object, you must select a font before it can be used in an output operation. The SelectObject function selects the font you have created and returns a handle to the previous font. The system stock font is always available, even if no other stock font is. If no other stock fonts are available, GetStockObject returns a handle to the system font.