Change default dialog font


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


Sometimes it is desirable to change the default font specified in dialog templates (usually "MS Sans Serif", 8 pts.) at runtime (dynamically). For example, you may want to increase the font size to make it more readable under higher screen resolutions. MFC library contains a class CDialogTemplate, that serves exactly this purpose, but MS has not bothered to include its description in standard MFC reference. This is how you can use this class in your code:

1. place the following string somewhere in your "stdafx.h" file:

#include <afxpriv.h>

2. override DoModal() function in your dialog class:

int CSimpleDialog::DoModal() 
   CDialogTemplate dlt;
   int             nResult;

   // load dialog template
   if (!dlt.Load(MAKEINTRESOURCE(CSimpleDialog::IDD))) return -1;

   // set your own font, for example "Arial", 10 pts. 
   dlt.SetFont("Arial", 10);
   // get pointer to the modified dialog template
   LPSTR pdata = (LPSTR)GlobalLock(dlt.m_hTemplate);
   // let MFC know that you are using your own template
   m_lpszTemplateName = NULL;

   // display dialog box
   nResult = CDialog::DoModal();

   // unlock memory object

   return nResult;	

It may be reasonable to choose a font for your dialog box according to user-specified schemes (those in Control Panel / Display / Appearance). Unfortunately I was unable to find any simple ways to get font settings for the dialog boxes. A possible alternative is to use font settings for icon titles and some related controls (like tree and list controls), that can be retrieved by SystemParametersInfo() function. Here is a simple procedure that returns the face name and the size in points for this font:

void GetSystemIconFont(CString& strFontName,int& nPointSize)
   LOGFONT lf;

   // get LOGFONT structure for the icon font
   // getting number of pixels per logical inch
   // along the display height
   HDC hDC    = ::GetDC(NULL);
   int nLPixY = GetDeviceCaps(hDC, LOGPIXELSY);

   // copy font parameters
   nPointSize  = -MulDiv(lf.lfHeight,72,nLPixY);
   strFontName = lf.lfFaceName;

Date Last Updated: April 3, 1999


  • Thank You

    Posted by Oliver M. on 05/11/2005 12:55pm

    That was exactly what I was looking for; thank you for sharing your knowledge!

  • Get font set in Control Panel/Display/Appearance

    Posted by afilipovik on 04/28/2004 05:51pm

    Use SystemParametersInfo function with uiAction = SPI_GETNONCLIENTMETRICS. Fonts are available from NONCLIENTMETRICS structure members.

  • Using ActiveX controls

    Posted by Legacy on 07/24/2003 07:00am

    Originally posted by: Jesse Krebs

    This is an excellent example. But it fails to work if the dialog has an Active X control on it. See MSDN Knowledge Base article Q231591, "HOWTO: Use a Dialog Template to Create a MFC Dialog with an ActiveX Control."

  • How can i add all available font in a combo box

    Posted by Legacy on 02/19/2003 08:00am

    Originally posted by: Ravi Sharma

    How can i add all available fonts in a combo box as in a Combo box in cFontDialog Class,i want ot create my own dialog box where i need to use this COMBO box,

  • Combo Boxes empty (they really are!!)

    Posted by Legacy on 01/28/2003 08:00am

    Originally posted by: moodboom

    I agree with Maria's post


    This is a great class and solves all my problems, except that the combo boxes are empty. I am going to try cutting and pasting from CDialog::DoModal() to solve the problem... anyone else go through this yet?

  • How to change font for a particular control

    Posted by Legacy on 01/25/2003 08:00am

    Originally posted by: Mohan Prakash

    I wrote the application to change the font for a particular controls in a dialog box. It is working under windows 98, but not working under window 2000 professional.
    The code i wrote

    LOGFONT lf;

    lf.lfWeight = 1;

    lf.lfHeight = 12;

    //create a font with logfont structure
    CFont myfont;


    //Find all the child controls in this dialog
    CWnd *m_pDlgControls = GetWindow(GW_CHILD);

    UINT nObjectId = m_pDlgControls->GetDlgCtrlID();
    int temp = strLabel.LoadString(nObjectId);
    case ID_SOME_CONTROLS // some controls
    m_pDlgControls = m_pDlgControls->GetWindow(GW_HWNDNEXT);

  • how to makedifferent font sizes...

    Posted by Legacy on 10/04/2002 07:00am

    Originally posted by: Kanat


    I write an SDI application which calls Access database
    I want some "statics" be in large size and some in small
    and print this current page wich shows only one personal data.
    Is it possible? If it is then could you help with that

    thank you furthermore.

  • How about this method...

    Posted by Legacy on 09/06/2002 07:00am

    Originally posted by: Mak

    I am a newbie, I am not sure this method is better, please comment.

    I used to put following codes into my OnInitDialog() to change the dialog font.

    CWnd *pWnd = GetWindow(GW_CHILD) ;

    while ( pWnd )
    pWnd->SetFont(&m_MyFont) ;
    pWnd = pWnd->GetNextWindow() ;

    • Changing the font of a dialog box

      Posted by Veeresh Pabbu on 01/15/2015 06:53am

      Good one @Legacy. Its working

  • Now for the Windows API way

    Posted by Legacy on 07/10/2002 07:00am

    Originally posted by: Brandon

    Ok, I've been playing with Dialogs now and I the following problem. I want to have a dialog, but I want it in the main window only. I don't to make a modal dialog and have that as the main dialog. I know how to make a window in a dynamic dialog. CreateWindow("BUTTON"... and all that, but it looks like I'm running 3.1 (I'm running 2000 currently). Then if I run it under XP, it looks like it should. Is this a problem with the standard API functions or is there something that I can do to change it? I want it to look like the standard button types, edit controls, and all that.

  • How to change the font on the fly using the same technic?

    Posted by Legacy on 04/19/2002 07:00am

    Originally posted by: boulifb

    Hi guys,

    How can we change the font of the dialog box on the fly when answering to the WM_SETTINGSCHANGED message?

    Best regards.


  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date