CREATING, SELECTING,AND DELETING LOGICAL FONTS

Now that we've nailed down the concept of logical inches, it's time to talk about logical fonts. The logical font is the sixth and final type of GDI object.

A logical font is the description of a font. Like the logical pen and logical brush, it is an abstract item that becomes real only when it is selected into a device context. For logical pens (for instance), you can specify any color you want for the pen, but Windows converts that to a pure color when you select the pen into the device context. Only then does Windows know about the color capabilities of the device.

With fonts, this distinction between the logical font that you request and the real font that you get is much more important, because the logical font and the real font can be very different. For example, suppose you request a 32-point Zapf Chancery font. Windows returns to your program a handle to a logical font. Now you select that font into a device context. What happens? It depends. If the device context is a printer device context for an Apple LaserWriter Plus, you will indeed be able to write text to the printer using a 32-point Zapf Chancery font. But if you select this logical font into your screen device context, you'll get something that only approximates this font.

This is the process for creating, selecting, and deleting logical fonts:

1.Create a logical font by calling CreateFont or CreateFontIndirect. These functions return a handle to a font of type HFONT.

2.Select the logical font into the device context using SelectObject. Windows chooses a real font that matches most closely the logical font.

3.Determine the size and characteristics of the real font with GetTextMetrics. (You can also get the name of the font with GetTextFace.) The information lets you properly space the text that you write when this font is selected into the device context.

4.Delete the logical font by calling DeleteObject. Don't delete the font while it is selected in a valid device context, however, and never delete stock fonts.

Windows has two functions for creating logical fonts. The first is:

hFont = CreateFont (nHeight . . . lpszFaceName) ;

The CreateFont function has more parameters than any other Windows function—14 of them. The 14 parameters to CreateFont correspond directly to the 14 fields of the LOGFONT structure. You can also create a logical font using the LOGFONT structure and the CreateFontIndirect function:

LOGFONT logfont ;

[other program lines]

hFont = CreateFontIndirect (&logfont) ;

In most cases, using CreateFontIndirect and the logical font structure is neater and more efficient than specifying the 14 parameters to CreateFont.

After you create a logical font, you select it into your device context with SelectObject:

hFontOld = SelectObject (hdc, hFont) ;

Windows then matches the logical font with a real font. You can determine the name of the typeface by using the function:

GetTextFace (hdc, sizeof szFaceName, szFaceName) ;

where szFaceName is a character array to receive the name. You can have Windows copy the various sizes of the font into a structure of type TEXTMETRIC using the familiar:

GetTextMetrics (hdc, &tm) ;

The GetObject function, which you can use to obtain information about a logical pen, brush, or bitmap, can also be used for logical fonts:

GetObject (hFont, sizeof (LOGFONT), &logfont) ;

But this function returns only the information that you put into logfont to create the font in the first place.

You can delete a logical font (but not while it is selected in a device context) with DeleteObject:

DeleteObject (hFont) ;