Monday, December 31, 2007

Change Default Dialog Font of CDialog

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

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;

}

1 comment:

Unknown said...
This comment has been removed by the author.