Font Dialog Box
The Font dialog box lets the user choose attributes for a logical font, such as font family and associated font style, point size, effects (underline, strikeout, and text color), and a script (or character set).
The following screen shot shows a typical Font dialog box.
If the user cancels the Font dialog box or an error occurs, ChooseFont returns FALSE and the contents of the LOGFONT structure are not defined. You can determine the cause of an error by using the CommDlgExtendedError function to retrieve the extended error value.
The following topics are discussed in this section.
- Font Dialog Box Initialization Flags
- Customizing the Font Dialog Box on earlier versions of Windows
- Customizing the Font Dialog Box on Windows 7
Before calling ChooseFont, the Flags member of the CHOOSEFONT structure must specify CF_SCREENFONTS, CF_PRINTERFONTS, or CF_BOTH, to indicate whether the dialog box should list screen fonts, printer fonts, or both. If you specify CF_PRINTERFONTS or CF_BOTH, the hDC member of the CHOOSEFONT structure must specify a handle to a device context for the printer.
If the CF_PRINTERFONTS or CF_BOTH flag is set, the font type description label appears at the bottom of the Font dialog box.
Starting with Windows 7, the CF_PRINTERFONTS, CF_SCREENFONTS, CF_BOTH, and CF_WYSIWYG flags are no longer used by the ChooseFont function for font enumeration. They are obsolete in Windows 7. However, the CF_PRINTERFONTS flag retains one function: to display the font type description label at the bottom of the Font dialog box.
You can use the Flags member to enable or disable some of the Font dialog box controls, and you can use the Flags member in conjunction with other CHOOSEFONT members to control the initial values of some controls.
To display the controls that allow the user to select strikeout, underline, and color options:
- Set the CF_EFFECTS flag. You can use the rgbColors member of the CHOOSEFONT structure to specify an initial font color.
To specify the initial values for the Font, Font Style, Size, Strikeout, and Underline dialog box controls:
- To specify the initial values for the Font, Font Style, Size, Strikeout, and Underline dialog box controls:
- Set the CF_INITTOLOGFONTSTRUCT flag in the Flags member, along with members of the LOGFONT structure that is pointed to by the lpLogFont, to specify the initial values for the font attributes.
- You can also use the CF_NOFACESEL, CF_NOSTYLESEL, and CF_NOSIZESEL flags to prevent the Font dialog box from displaying initial values for the corresponding controls. This is useful when you are working with a selection of text that has more than one typeface, style, or point size. These values will also be set in Flags when ChooseFont returns, if the user did not select a corresponding value.
To initialize the Font Style control to a specified style name
- Set the CF_USESTYLE flag and use the lpszStyle member to specify the style name.
Note To globalize your application, specify the style by using the lfWeight and lfItalic members of the LOGFONT structure that is pointed to by lpLogFont. The style name may change depending on the system user interface language.
To display the Apply button
- Set the CF_APPLY flag and provide a hook procedure to process WM_COMMAND messages for the Apply button. The hook procedure can send the WM_CHOOSEFONT_GETLOGFONT message to the dialog box to retrieve the address of the LOGFONT structure that contains the current selections for the font.
To display the Help button
- Set the CF_SHOWHELP flag. The hwndOwner member must identify the window to receive the HELPMSGSTRING registered message when the user clicks the Help button.
To restrict the fonts displayed in the dialog box
- Set any combination of the CF_TTONLY, CF_FIXEDPITCHONLY, CF_NOVECTORFONTS, CF_NOVERTFONTS, CF_SCALABLEONLY, and CF_WYSIWYG flags. You can also restrict the available styles that the dialog box displays for some fonts by using the CF_NOSIMULATIONS value.
Starting with Windows 7, the list of fonts displayed in the dialog box is restricted based on the user's shown fonts. To remove the restriction, set the CF_INACTIVEFONTS flag.
To restrict the typeface names, styles, and point sizes that the user can specify
- Set the CF_FORCEFONTEXIST flag to restrict the user to specifying only valid typeface names, styles, and point sizes listed in the dialog box.
- Set the CF_LIMITSIZE flag to restrict the user to specifying point sizes in the range specified by the nSizeMin and nSizeMax members.
To restrict or disable the Scripts combo box
- Set the CF_NOSCRIPTSEL flag to disable the Scripts combo box, or set the CF_SELECTSCRIPT flag to restrict selections in the Scripts combo box to a specified character set.
You can provide a custom template for the Font dialog box, for example, if you want to include additional controls that are unique to your application. The ChooseFont function uses your custom template in place of the default template.
To provide a custom template for the Font dialog box
- Create the custom template by modifying the default template specified in the Font.dlg file. The control identifiers used in the default Font dialog template are defined in the Dlgs.h file.
- Use the CHOOSEFONT structure to enable the template as follows:
- If your custom template is a resource in an application or dynamic link library, set the CF_ENABLETEMPLATE flag in the Flags member. Use the hInstance and lpTemplateName members of the structure to identify the module and resource name.
- If your custom template is already in memory, set the CF_ENABLETEMPLATEHANDLE flag. Use the hInstance member to identify the memory object that contains the template.
You can provide a CFHookProc hook procedure for the Font dialog box. The hook procedure can process messages sent to the dialog box and send messages to the dialog box. If you use a custom template to define additional controls, you must provide a hook procedure to process input for your controls.
To enable a hook procedure for the Font dialog box
- Set the CF_ENABLEHOOK flag in the Flags member of the CHOOSEFONT structure.
- Specify the address of the hook procedure in the lpfnHook member.
After processing the WM_INITDIALOG message, the dialog box procedure sends a WM_INITDIALOG message to the hook procedure. The lParam parameter of this message is a pointer to the CHOOSEFONT structure that is used to initialize the dialog box.
The hook procedure can send the WM_CHOOSEFONT_GETLOGFONT, WM_CHOOSEFONT_SETLOGFONT, and WM_CHOOSEFONT_SETFLAGS messages to the dialog box to get and set the current values and flags of the dialog box.
The following screen shot shows a typical Font dialog box in Windows 7.
In earlier Windows versions, the font.dlg template file contains one default ChooseFont template. The font.dlg template file on Windows 7 contains two default templates: the default template from earlier Windows versions and the new Windows 7 ChooseFont template. Therefore, when you customize the Font dialog box on Windows 7, you must consider the following issues.
Use the new template when you create custom templates for applications that run on Windows 7. This new template contains a link control that the user can click to launch the Fonts Control Panel window, as shown in the following screen shot.
- To use this link control, your calling application must use the COMCTL32.DLL version 6 or later. Otherwise, the ChooseFont function returns an error when it tries to create the link control in your custom template. To determine if this control is enabled, compile your calling application against COMCTL32.DLL version 6.0. For more information, see Enabling Visual Styles with Common Controls.
If your application uses COMCTL32.DLL version 5.0 or earlier, you must do the following when you create a custom template:
- Specify the control as a PUSHBUTTON. The control used to launch the Fonts Control Panel will appear as a button rather than as a link.
- Replace the following text in the font.dlg:
CONTROL "<A>Show more fonts</A>", IDC_MANAGE_LINK, "SysLink", WS_TABSTOP, 7, 199, 227, 9
with the following text:
PUSHBUTTON "S&how more fonts", IDC_MANAGE_LINK, 7, 199, 74, 14 , WS_TABSTOP
To ensure that your application uses a custom template, you must specify a custom template with the CF_ENABLETEMPLATE flag, create a custom template based on the Windows 7 ChooseFont template, and then optionally enable a hook procedure.
If you enable a hook procedure without creating a custom template, the default ChooseFont template for earlier Windows versions will be loaded.
Note You must specify the CONTROL or PUSHBUTTON control type in your new template, depending on the version of COMMCTL.DLL that your application compiles against. Also note that Windows 7 specific features, such as WYSIWYG display of font lists and extended families, are not available when your applications run on earlier versions of the Windows operating system.