Home

Windows Controls: The Font Dialog Box

 

Introduction to the Font Dialog Box

 

Description

To support easy selection of font, Microsoft Windows provides the Font dialog box:

 

To use the Font dialog box, the user should first have text that needs to, and can, be formatted. Once the dialog box displays, a user can select a font by its name, its style, its size, one or both effects (Underline or Strikeout), and a color. After making the necessary changes, the user can click OK to apply the changes or click Cancel to ignore the selected attributes.

Creating a Font Dialog Box

To suport the font dialog box, the MFC library provides a class named CFontDialog. The CFontDialog class is derived from the CCommonDialog class.

The CFontDialog class is equipped with two constructors whose syntaxes are:

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 
);

To create a font dialog box, declare variable or a pointer of type CFontDialog. This is can be done in the function or event where the dialog box would be needed. Here is an example:

void CExerciseDlg::OnBnClickedFont()
{
	// TODO: Add your control notification handler code here
	CFontDialog dlg;
}

If you want the dialog box to be available to all functions and events of a unit, you can declare its variable in the header of the class that would use it.

To support the font dialog box, the Win32 library provides a structure named CHOOSEFONT.

Characteristics of a Font Dialog Box

   

Introduction

At design time, the font dialog box hardly needs any change of properties to work. The only time you would set its properties is if you judge that its default properties are not conform to your particular scenario. For example, if you are providing font formatting for a control and you want users to control the font characteristics of individual letters or paragraph, there should be nothing to change at design time.

To support a relationship with the Win32's font dialog box, the CFontDialog class is equipped with a member variable named m_cf:

CHOOSEFONT m_cf;

Once, and however, you have a CFontDialog instance, you can display the Font dialog box by calling the DoModal() member function. The font dialog box is equipped with two primary buttons: OK and Cancel. After using it, if the user clicks OK, this implies that if there were changes of font, size, color, etc, the user wants them committed to the document. If the user clicks Cancel, this means that you should ignore any actions that were performed on the dialog box. The DoModal() member function is of type INT_PTR. It returns IDOK if the user clicks OK. Otherwise, if the user clicks Cancel, it would return IDCANCEL. Therefore, after the user has used it, you should find out if she clicked OK or Cancel before applying her changes. This inquiry is usually performed with an if conditional statement as follows:

if( dlgFont->DoModal() == IDOK )
{
	// What to do if the user clicked OK
}

The Font From the Dialog Box

As its name indicates, the font dialog box is used to configure or get a font. To support its characteristics, the CFontDialog class is equipped with various member functions. After the user has used a font dialog box and clicked OK, you can find out what characteristics were set.

 
 
     
 

Home Copyright © 2010-2016, FunctionX