Microsoft KB Archive/85518

= INFO: Correct Use of the SetFont Function in MFC =

Article ID: 85518

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft C/C++ Professional Development System 7.0

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51</li></ul>

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q85518

<div class="notice_section">

<div class="summary_section">

SUMMARY
The SetFont member function of the CWnd class changes the font in a specified control. For this function to work correctly in a Windows- based application, you must ensure that the CFont object specified in the SetFont call is not destroyed until after the specified control has been destroyed.

<div class="moreinformation_section">

MORE INFORMATION
The SetFont accepts a CFont object as a parameter; the control uses the specified font to display its text. The SetFont function is implemented to send a WM_SETFONT message to the control with the font handle that corresponds to the CFont object.

An application can delete the font specified in a WM_SETFONT message only under certain circumstances; these same restrictions apply to the CFont object specified in a SetFont call.

Specifically, do not destroy the specified CFont object until after the CWnd control has been destroyed. Windows does not copy the font specified in a SetFont call. If the font is destroyed before the control is destroyed, unpredictable results can occur.

For example, when an application uses SetFont to change the font a dialog box uses, the application should not destroy the CFont object until after it has destroyed the dialog box. Make the CFont object a member variable of the derived dialog box class instead of making the font a local variable in one of the functions in the class. This is the best method to ensure that the CFont object exists for the lifetime of the dialog box. When the application destroys the dialog box, the dialog box class destructor automatically calls the CFont destructor to delete the font handle.

The best time to specify the font for any controls in the dialog box is in the OnInitDialog member function.

The code below demonstrates deriving a dialog box class from CModalDialog and using the SetFont member function:

Sample Code
/* * Compiler options needed: None */

class CMyAboutBox : public CDialog {  CFont m_font;

public: // Constructor -- This code assumes a dialog box // template named &quot;ABOUTDLG&quot; in the application's .RC file.

CMyAboutBox(CWnd* pParentWnd = NULL) : CModalDialog(&quot;ABOUTDLG&quot;, pParentWnd) {};

BOOL OnInitDialog; };

// OnInitDialog function -- Called after Windows creates // the dialog box but before it is painted on the screen.

BOOL CMyAboutBox::OnInitDialog {  LOGFONT lf;                        // Used to create the CFont.

CDialog::OnInitDialog;          // Call default ::OnInitDialog

memset(&lf, 0, sizeof(LOGFONT));  // Clear out structure. lf.lfHeight = 20;                 // Request a 20-pixel-high font strcpy(lf.lfFaceName, &quot;Arial&quot;);   //    with face name &quot;Arial&quot;. m_font.CreateFontIndirect(&lf);   // Create the font.

// Use the font to paint a control. This code assumes // a control named IDC_TEXT1 in the dialog box. GetDlgItem(IDC_TEXT1)->SetFont(&m_font);

return TRUE; }

Additional query words: kbinf 7.00 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10 4.00 destructor

Keywords: kbinfo kbuidesign KB85518

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.