Font Dialog Box


The Font dialog box lets the user choose attributes for a logical font, such as typeface name, style (bold, italic, or regular), point size, effects (underline, strikeout, and text color), and a script (or character set).

You create and display a Font dialog box by initializing a CHOOSEFONT structure and passing the structure to the ChooseFont function.

The following illustration shows a typical Font dialog box.

If the user clicks the OK button, ChooseFont returns TRUE and sets the members of the LOGFONT structure pointed to by the lpLogFont member of the CHOOSEFONT structure. You can use the LOGFONT structure with the function to create a logical font. ChooseFont also sets other CHOOSEFONT members to indicate the user's selections.

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 Initialization Flags

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.

You can use the Flags member to enable or disable some of the 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

To specify the initial values of the Font, Font Style, Size, Strikeout, and Underline dialog box controls

To initialize the Font Style control to a specified style name

To display the Apply button

To display the Help button

To restrict the fonts the dialog box displays

To restrict the typeface names, styles, and point sizes that the user can specify

To restrict or disable the Scripts combo box

Customizing the Font Dialog Box

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

  1. 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.
  2. 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.

         

        -Or-

         

      • 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 CCHookProc hook procedure for the Font dialog box. The hook procedure can process messages sent to the dialog box. It can also 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

  1. Set the CF_ENABLEHOOK flag in the Flags member of the CHOOSEFONT structure.
  2. Specify the address of the hook procedure in the lpfnHook member.

After processing its 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 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.

Choosing a Font

This topic describes sample code that displays a Font dialog box so a user can choose the attributes of a font. The sample code first initializes a CHOOSEFONT structure, and then calls the ChooseFont function to display the dialog box.

This example sets the CF_SCREENFONTS flag to specify that the dialog box should display only screen fonts. It sets the CF_EFFECTS flag to display controls that allow the user to select strikeout, underline, and color options.

If ChooseFont returns TRUE, indicating that the user clicked the OK button, the structure pointed to by the lpLogFont member of the CHOOSEFONT structure contains information that describes the font and font attributes selected by the user. The rgbColors member contains the selected text color. The sample code uses this information to set the font and text color for the device context associated with the owner window.