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



Comments

  • Thank You

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

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

    Reply
  • Get font set in Control Panel/Display/Appearance

    Posted by afilipovik on 04/28/2004 10:51am

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

    Reply
  • Using ActiveX controls

    Posted by Legacy on 07/24/2003 12: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."

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

    Posted by Legacy on 02/19/2003 12: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,

    Reply
  • Combo Boxes empty (they really are!!)

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

    Originally posted by: moodboom

    I agree with Maria's post

    http://www.codeguru.com/mfc/comments/24092.shtml

    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?

    Reply
  • How to change font for a particular control

    Posted by Legacy on 01/25/2003 12: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;
    memset(&lf,0,sizeof(LOGFONT));
    lstrcpy(lf.lfFaceName,"Arial");

    lf.lfWeight = 1;

    lf.lfHeight = 12;

    //create a font with logfont structure
    CFont myfont;
    myfont.CreatePointFontIndirect(&lf);

    dc.SelectObject(&myfont);


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

    while(m_pDlgControls)
    {
    UINT nObjectId = m_pDlgControls->GetDlgCtrlID();
    int temp = strLabel.LoadString(nObjectId);
    switch(nObjectId)
    {
    case ID_SOME_CONTROLS // some controls
    m_pDlgControls->SetFont(&myfont);
    m_pDlgControls->SetWindowText(strLabel);
    break;
    }
    m_pDlgControls = m_pDlgControls->GetWindow(GW_HWNDNEXT);
    }


    Reply
  • how to makedifferent font sizes...

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

    Originally posted by: Kanat

    sir/madam

    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.

    Reply
  • How about this method...

    Posted by Legacy on 09/06/2002 12: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/14/2015 10:53pm

      Good one @Legacy. Its working

      Reply
    Reply
  • Now for the Windows API way

    Posted by Legacy on 07/10/2002 12: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.

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

    Posted by Legacy on 04/19/2002 12: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.

    Fred.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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