The Font common dialog box displays lists of typefaces, styles, and point sizes that correspond to the available fonts. After the user selects the current font for an application, the dialog box displays sample text rendered with that font, as shown in Figure 6-7.
Figure 6-7.
To use the Font common dialog box, you fill out the CHOOSEFONT structure and call the ChooseFont function. The CHOOSEFONT structure contains information such as the attributes of the initial font, the point size, and the types of fonts (screen fonts or printer fonts), as well as hook and template information.
The following code demonstrates how the CMNDLG32 sample fills out the CHOOSEFONT structure. In the sample, how some of the fields are filled depends on whether a hook or a template should be included. The flags indicate that the strikeout, underline, and color effects should be enabled (CF_EFFECTS) and that the fonts listed should include only the screen fonts supported by the system (CF_SCREENFONTS). Note that, although it is not used in this sample, the CF_NOSCRIPTSEL flag is available if you want to gray out the script box.
CHOOSEFONT chf;
LOGFONT lf;
§
HDC hDC;
hDC = GetDC (hWnd);
chf.hDC = CreateCompatibleDC (hDC);
ReleaseDC (hWnd, hDC);
chf.lStructSize = sizeof (CHOOSEFONT);
chf.hwndOwner = hWnd;
chf.lpLogFont = &lf;
chf.Flags = CF_SCREENFONTS | CF_EFFECTS;
chf.rgbColors = RGB (0, 255, 255);
chf.lCustData = 0;
chf.hInstance = (HANDLE)hInst;
chf.lpszStyle = (LPTSTR)NULL;
chf.nFontType = SCREEN_FONTTYPE;
chf.nSizeMin = 0;
chf.nSizeMax = 0;
switch (wMode)
{
case IDM_STANDARD:
chf.lpfnHook = (LPCFHOOKPROC)(FARPROC)NULL;
chf.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_HOOK:
chf.Flags |= CF_ENABLEHOOK;
chf.lpfnHook = (LPCFHOOKPROC)ChooseFontHookProc;
chf.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_CUSTOM:
chf.Flags |= CF_ENABLEHOOK | CF_ENABLETEMPLATE;
chf.lpfnHook = (LPCFHOOKPROC)ChooseFontHookProc;
chf.lpTemplateName = (LPTSTR) MAKEINTRESOURCE (FORMATDLGORD31);
break;
}
After you fill out the structure, you simply call the ChooseFont function and delete the DC. The common dialog procedure sets the font for you. If you need to customize the dialog box, your application should provide a hook function and, if you want to add controls, a dialog template. Instead of specifying additional controls in a child dialog box, as you do for the Open and Save As common dialog boxes, you must include the full template for the Font common dialog box in order to customize it. For example, the CMNDLG32.RC file includes the following template for the Font dialog box:
1543 DIALOG DISCARDABLE 13, 54, 264, 147
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Font"
FONT 8, "Helv"
BEGIN
LTEXT "&Font:",1088,6,3,40,9
COMBOBOX 1136,6,13,94,54,CBS_SIMPLE | CBS_OWNERDRAWFIXED |
CBS_AUTOHSCROLL | CBS_SORT | CBS_HASSTRINGS |
CBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP
LTEXT "Font St&yle:",1089,108,3,44,9
COMBOBOX 1137,108,13,64,54,CBS_SIMPLE | CBS_DISABLENOSCROLL |
WS_VSCROLL | WS_TABSTOP
LTEXT "&Size:",1090,179,3,30,9
COMBOBOX 1138,179,13,32,54,CBS_SIMPLE | CBS_OWNERDRAWFIXED |
CBS_SORT | CBS_HASSTRINGS | CBS_DISABLENOSCROLL |
WS_VSCROLL | WS_TABSTOP
DEFPUSHBUTTON "OK",IDOK,218,6,40,14,WS_GROUP
PUSHBUTTON "Cancel",IDCANCEL,218,23,40,14,WS_GROUP
PUSHBUTTON "&Apply",1026,218,40,40,14,WS_GROUP
PUSHBUTTON "&Help",1038,218,57,40,14,WS_GROUP
GROUPBOX "Effects",1072,6,72,84,34,WS_GROUP
CONTROL "Stri&keout",1040,"Button",
BS_AUTOCHECKBOX | WS_TABSTOP,10,82,49,10
CONTROL "&Underline",1041,"Button",
BS_AUTOCHECKBOX,10,94,51,10
LTEXT "&Color:",1091,6,110,30,9
COMBOBOX 1139,6,120,84,100,
CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED |
CBS_AUTOHSCROLL | CBS_HASSTRINGS | WS_BORDER |
WS_VSCROLL | WS_TABSTOP
GROUPBOX "Sample",1073,98,72,160,49,WS_GROUP
CTEXT "",1093,98,124,160,20,SS_NOPREFIX | NOT WS_GROUP
CTEXT "AaBbYyZz",1092,104,81,149,37,SS_NOPREFIX | NOT
WS_VISIBLE
END