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?


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.


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.