How to change the default font of a control

22 01 2012

Each windows controls(Static Text, Edit, Button etc) have its own default fonts. Sometimes we may need to change this for a better visibility or some other usability requirements.

Before setting a font to a control, we have to create a CFont object with desired size and other parameters using CreateFont function. Then we can set that font to any controls using CWnd’s SetFont function.

CFont newFont;

void ChangeFont()
{
	/** Create a font*/

    newFont.CreateFont(20,
		20,
		0,
		0,
		FW_BOLD,
		FALSE,
		FALSE,
		0,
		DEFAULT_CHARSET,
		OUT_CHARACTER_PRECIS,
		CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH,
		NULL);

	/** Get the handle of a control*/
	CWnd* pCtrlWnd = GetDlgItem(IDC_ST_TEST_CONTROL);

	/** Set new font for that control*/
	pCtrlWnd->SetFont(&newFont, TRUE);
}

Font can be created in many ways, but the only important thing is font object should exist as long as the control exist. Otherwise font object should decalre as a member or a global object.

Advertisements

Actions

Information

One response

8 06 2014
Richard

And Also had to change newFont.CreateFont to newFont.CreateFontW.
Then al worked fine , also I used newFont.DeleteObject() to delete the font when needed.

Greetz

Richard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: