Gold mine of Visual C++ tricks!
How to set Font for Static Text Controls?
![]()
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.
| Print article | This entry was posted by Jijo Raj on March 4, 2009 at 5:57 pm, and is filed under Codeproject, Uncategorized, Visual C++. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |