How to set Font for Static Text Controls?

4 03 2009


By default, static text is displayed in normal fonts. And you don’t have any option to make it bold or italic or underline. Is there any way to enable these styles and change the font of the static text control?

staticfont


Yes! you can do it. First you’ve to get the current font of the text control and then enable the styles you need then set it back. Setting font is done in OnInitDialog() and new font is kept as member variable. See the code snippet below.

BOOL CStaticFontDlg::OnInitDialog()
{
	...

    // Get current font.
    CFont* pFont = GetDlgItem( IDC_STATIC_ITALIC )->GetFont();
    LOGFONT LogFont = { 0 };
    pFont->GetLogFont( &LogFont );

    // Create new font with underline style.
    LogFont.lfUnderline = TRUE;
    m_StaticFont.CreateFontIndirect( &LogFont );

	// Sets the new font back to static text.
    GetDlgItem( IDC_STATIC_ITALIC )->SetFont( &m_StaticFont );

	return TRUE;
}

Now the static text will look like this.

staticfont3


Don’t forget to change the Id of static text control from IDC_STATIC to something else. Or else GetDlgItem() will return invalid handle.


Targeted Audiance – Intermediate.

Advertisements

Actions

Information

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s




%d bloggers like this: