Change default dialog font | CodeGuru

Change default dialog font

. 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 3, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

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;
   InitModalIndirect(pdata);

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

   // unlock memory object
   GlobalUnlock(dlt.m_hTemplate);

   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
   SystemParametersInfo(SPI_GETICONTITLELOGFONT,sizeof(LOGFONT),&lf,0);

   // getting number of pixels per logical inch
   // along the display height
   HDC hDC    = ::GetDC(NULL);
   int nLPixY = GetDeviceCaps(hDC, LOGPIXELSY);
   ::ReleaseDC(NULL,hDC);

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

Date Last Updated: April 3, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.