CFontDialog Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CFontDialog Class.

Allows you to incorporate a font-selection dialog box into your application.

Syntax

class CFontDialog : public CCommonDialog  

Members

Public Constructors

Name Description
CFontDialog::CFontDialog Constructs a CFontDialog object.

Public Methods

Name Description
CFontDialog::DoModal Displays the dialog and allows the user to make a selection.
CFontDialog::GetCharFormat Retrieves the character formatting of the selected font.
CFontDialog::GetColor Returns the color of the selected font.
CFontDialog::GetCurrentFont Assigns the characteristics of the currently selected font to a LOGFONT structure.
CFontDialog::GetFaceName Returns the face name of the selected font.
CFontDialog::GetSize Returns the point size of the selected font.
CFontDialog::GetStyleName Returns the style name of the selected font.
CFontDialog::GetWeight Returns the weight of the selected font.
CFontDialog::IsBold Determines whether the font is bold.
CFontDialog::IsItalic Determines whether the font is italic.
CFontDialog::IsStrikeOut Determines whether the font is displayed with strikeout.
CFontDialog::IsUnderline Determines whether the font is underlined.

Public Data Members

Name Description
CFontDialog::m_cf A structure used to customize a CFontDialog object.

Remarks

A CFontDialog object is a dialog box with a list of fonts that are currently installed in the system. The user can select a particular font from the list, and this selection is then reported back to the application.

To construct a CFontDialog object, use the provided constructor or derive a new subclass and use your own custom constructor.

Once a CFontDialog object has been constructed, you can use the m_cf structure to initialize the values or states of controls in the dialog box. The m_cf structure is of type CHOOSEFONT. For more information on this structure, see the Windows SDK.

After initializing the dialog object's controls, call the DoModal member function to display the dialog box and allow the user to select a font. DoModal returns whether the user selected the OK ( IDOK) or Cancel ( IDCANCEL) button.

If DoModal returns IDOK, you can use one of CFontDialog's member functions to retrieve the information input by the user.

You can use the Windows CommDlgExtendedError function to determine whether an error occurred during initialization of the dialog box and to learn more about the error. For more information on this function, see the Windows SDK.

CFontDialog relies on the COMMDLG.DLL file that ships with Windows versions 3.1 and later.

To customize the dialog box, derive a class from CFontDialog, provide a custom dialog template, and add a message-map to process the notification messages from the extended controls. Any unprocessed messages should be passed to the base class.

Customizing the hook function is not required.

For more information on using CFontDialog, see Common Dialog Classes.

Inheritance Hierarchy

CObject

CCmdTarget

CWnd

CDialog

CCommonDialog

CFontDialog

Requirements

Header: afxdlgs.h

CFontDialog::CFontDialog

Constructs a CFontDialog object.

CFontDialog(
    LPLOGFONT lplfInitial = NULL,  
    DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,  
    CDC* pdcPrinter = NULL,  
    CWnd* pParentWnd = NULL);

CFontDialog(
    const CHARFORMAT& charformat,  
    DWORD dwFlags = CF_SCREENFONTS,  
    CDC* pdcPrinter = NULL,  
    CWnd* pParentWnd = NULL);  

Parameters

l plfInitial
A pointer to a LOGFONT data structure that allows you to set some of the font's characteristics.

charFormat
A pointer to a CHARFORMAT data structure that allows you to set some of the font's characteristics in a rich edit control.

dwFlags
Specifies one or more choose-font flags. One or more preset values can be combined using the bitwise OR operator. If you modify the m_cf.Flags structure member, be sure to use a bitwise OR operator in your changes to keep the default behavior intact. For details on each of these flags, see the description of the CHOOSEFONT structure in the Windows SDK.

pdcPrinter
A pointer to a printer-device context. If supplied, this parameter points to a printer-device context for the printer on which the fonts are to be selected.

pParentWnd
A pointer to the font dialog box's parent or owner window.

Remarks

Note that the constructor automatically fills in the members of the CHOOSEFONT structure. You should only change these if you want a font dialog different than the default.

Note

The first version of this function only exists when there is no rich edit control support.

Example

   // Show the font dialog with all the default settings.
   CFontDialog dlg;
   dlg.DoModal();

   // Show the font dialog with 12 point "Times New Roman" as the 
   // selected font.
   LOGFONT lf;
   memset(&lf, 0, sizeof(LOGFONT));

   CClientDC dc(this); // expects a CWnd that has already been initialized
   lf.lfHeight = -MulDiv(12, dc.GetDeviceCaps(LOGPIXELSY), 72);
   _tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("Times New Roman"));

   CFontDialog fdlg(&lf);
   fdlg.DoModal();

CFontDialog::DoModal

Call this function to display the Windows common font dialog box and allow the user to choose a font.

virtual INT_PTR DoModal();

Return Value

IDOK or IDCANCEL. If IDCANCEL is returned, call the Windows CommDlgExtendedError function to determine whether an error occurred.

IDOK and IDCANCEL are constants that indicate whether the user selected the OK or Cancel button.

Remarks

If you want to initialize the various font dialog controls by setting members of the m_cf structure, you should do this before calling DoModal, but after the dialog object is constructed.

If DoModal returns IDOK, you can call other member functions to retrieve the settings or information input by the user into the dialog box.

Example

See the examples for CFontDialog::CFontDialog and CFontDialog::GetColor.

CFontDialog::GetCharFormat

Retrieves the character formatting of the selected font.

void GetCharFormat(CHARFORMAT& cf) const;  

Parameters

cf
A CHARFORMAT structure containing information about the character formatting of the selected font.

CFontDialog::GetColor

Call this function to retrieve the selected font color.

COLORREF GetColor() const;  

Return Value

The color of the selected font.

Example

   // Get the color of the selected font, if any.
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      COLORREF color = dlg.GetColor();
      TRACE(_T("Color of the selected font = %8x\n"), color);
   }

CFontDialog::GetCurrentFont

Call this function to assign the characteristics of the currently selected font to the members of a LOGFONT structure.

void GetCurrentFont(LPLOGFONT lplf);

Parameters

lplf
A pointer to a LOGFONT structure.

Remarks

Other CFontDialog member functions are provided to access individual characteristics of the current font.

If this function is called during a call to DoModal, it returns the current selection at the time (what the user sees or has changed in the dialog). If this function is called after a call to DoModal (only if DoModal returns IDOK), it returns what the user actually selected.

Example

   // Get the characteristics of the currently selected font, if any.
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      LOGFONT lf;
      dlg.GetCurrentFont(&lf);
      TRACE(_T("Face name of the selected font = %s\n"), lf.lfFaceName);
   }

CFontDialog::GetFaceName

Call this function to retrieve the face name of the selected font.

CString GetFaceName() const;  

Return Value

The face name of the font selected in the CFontDialog dialog box.

Example

   // Get the face name of the selected font, if any.
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      CString facename = dlg.GetFaceName();
      TRACE(_T("Face name of the selected font = %s\n"), facename);
   }

CFontDialog::GetSize

Call this function to retrieve the size of the selected font.

int GetSize() const;  

Return Value

The font's size, in tenths of a point.

Example

   // Get the size of the selected font, if any.
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      int size = dlg.GetSize();
      TRACE(_T("The size of the selected font = %d\n"), size);
   }

CFontDialog::GetStyleName

Call this function to retrieve the style name of the selected font.

CString GetStyleName() const;  

Return Value

The style name of the font.

Example

   // Get the style name of the selected font, if any.
   CFontDialog dlg;
   dlg.m_cf.Flags |= CF_USESTYLE;
   if (dlg.DoModal() == IDOK)
   {
      CString stylename = dlg.GetStyleName();
      TRACE(_T("Style name of the selected font = %s\n"), stylename);
   }

CFontDialog::GetWeight

Call this function to retrieve the weight of the selected font.

int GetWeight() const;  

Return Value

The weight of the selected font.

Remarks

For more information on the weight of a font, see CFont::CreateFont.

Example

   // Get the weight of the selected font, if any.
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      int weight = dlg.GetWeight();
      TRACE(_T("Weight of the selected font = %d\n"), weight);
   }

CFontDialog::IsBold

Call this function to determine if the selected font is bold.

BOOL IsBold() const;  

Return Value

Nonzero if the selected font has the Bold characteristic enabled; otherwise 0.

Example

   // Is the selected font bold?
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      BOOL bold = dlg.IsBold();
      TRACE(_T("Is the selected font bold? %d\n"), bold);
   }

CFontDialog::IsItalic

Call this function to determine if the selected font is italic.

BOOL IsItalic() const;  

Return Value

Nonzero if the selected font has the Italic characteristic enabled; otherwise 0.

Example

   // Is the selected font italic?
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      BOOL italic = dlg.IsItalic();
      TRACE(_T("Is the selected font italic? %d\n"), italic);
   }

CFontDialog::IsStrikeOut

Call this function to determine if the selected font is displayed with strikeout.

BOOL IsStrikeOut() const;  

Return Value

Nonzero if the selected font has the Strikeout characteristic enabled; otherwise 0.

Example

   // Is the selected font displayed with strikeout?
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      BOOL strikeout = dlg.IsStrikeOut();
      TRACE(_T("Is the selected font strikeout? %d\n"), strikeout);
   }

CFontDialog::IsUnderline

Call this function to determine if the selected font is underlined.

BOOL IsUnderline() const;  

Return Value

Nonzero if the selected font has the Underline characteristic enabled; otherwise 0.

Example

   // Is the selected font underlined?
   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      BOOL underline = dlg.IsUnderline();
      TRACE(_T("Is the selected font underlined? %d\n"), underline);
   }

CFontDialog::m_cf

A structure whose members store the characteristics of the dialog object.

CHOOSEFONT m_cf;  

Remarks

After constructing a CFontDialog object, you can use m_cf to modify various aspects of the dialog box before calling the DoModal member function. For more information on this structure, see CHOOSEFONT in the Windows SDK.

Example

   // The code fragment creates a font based on the information 
   // we got from CFontDialog::m_cf variable.

   CFontDialog dlg;
   if (dlg.DoModal() == IDOK)
   {
      // Create the font using the selected font from CFontDialog.
      LOGFONT lf;
      memcpy(&lf, dlg.m_cf.lpLogFont, sizeof(LOGFONT));

      CFont font;
      VERIFY(font.CreateFontIndirect(&lf));

      // Do something with the font just created...
      CClientDC dc(this);
      CFont* def_font = dc.SelectObject(&font);
      dc.TextOut(5, 5, _T("Hello"), 5);
      dc.SelectObject(def_font);

      // Done with the font. Delete the font object.
      font.DeleteObject();
   }

See Also

MFC Sample HIERSVR
CCommonDialog Class
Hierarchy Chart