4.2.1 Displaying the Font Dialog Box in Your Application

The Font dialog box appears after you initialize the members in a CHOOSEFONT structure and call the ChooseFont function. This structure should be global or declared as a static variable. The members of the CHOOSEFONT structure contain information about such items as the following:

The attributes of the font that initially is to appear in the dialog box.

The attributes of the font that the user selected.

The point size of the font that the user selected.

Whether the list of fonts corresponds to a printer, a screen, or both.

Whether the available fonts listed are TrueType only.

Whether the Effects box should appear in the dialog box.

Whether dialog box messages should be processed by an application-supplied hook function.

Whether the point sizes of the selectable fonts should be limited to a specified range.

Whether the dialog box should display only what-you-see-is-what-you-get (WYSIWIG) fonts. (These fonts are resident on both the screen and the printer.)

The color that the ChooseFont function should use to render text in the Sample box the first time the application displays the dialog box.

The color that the user selected for text output.

To display the Font dialog box, an application should perform the following steps:

1.If the application requires printer fonts, retrieve a device-context handle for the printer and use this handle to set the hDC member of the CHOOSEFONT structure. (If the Font dialog box displays only screen fonts, this member should be set to NULL.)

2.Set the appropriate flags in the Flags member of the CHOOSEFONT structure. This setting must include CF_SCREENFONTS, CF_PRINTERFONTS, or CF_BOTH.

3.Set the rgbColors member of the CHOOSEFONT structure if the default color (black) is not appropriate.

4.Set the nFontType member of the CHOOSEFONT structure using the appropriate constant.

5.Set the nSizeMin and nSizeMax members of the CHOOSEFONT structure if the CF_LIMITSIZE value is specified in the Flags member.

6.Call the ChooseFont function.

The following example initializes the CHOOSEFONT structure and calls the ChooseFont function:

LOGFONT lf;
CHOOSEFONT cf;

/* Set all structure fields to zero. */

memset(&cf, 0, sizeof(CHOOSEFONT));

cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
cf.rgbColors = RGB(0, 255, 255); /* light blue */
cf.nFontType = SCREEN_FONTTYPE;

ChooseFont(&cf);

When the user closes the Font dialog box by choosing the OK button, the ChooseFont function returns information about the selected font in the LOGFONT structure to which the lpLogFont member points. An application can use this LOGFONT structure to select the font that will be used to render text. The following example selects a font by using the LOGFONT structure and renders a string of text:

hdc = GetDC(hwnd);
hFont = CreateFontIndirect(cf.lpLogFont);
hFontOld = SelectObject(hdc, hFont);
TextOut(hdc, 50, 150,
    "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", 52);
SelectObject(hdc, hFontOld);
DeleteObject(hFont);
ReleaseDC(hwnd, hdc);

An application can also use the WM_CHOOSEFONT_GETLOGFONT message to retrieve the current LOGFONT structure for the Font dialog box before the user closes the dialog box.