Click here to Skip to main content
11,925,730 members (61,997 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked


, 20 Jun 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
An easy way to change the look of a CStatic.

Sample Image - CFontStatic.gif


I was looking for a way to show text in different fonts one day, but I couldn't find any code that fitted the purpose for what I needed, so I decided to write a class for it. Since the class has been an asset to me when programming dialog-based applications, I've decided to release it here.

I know that this class isn't perfect, because there's always something that can be done more efficient; but if you have got any complaints or remarks, then constructive criticism is more than welcome.

CFontStatic: The class

The CFontStatic class is (as the name reveals) derived from CStatic but has three slight differences: the functions SetFontStyle, SetBackground, and SetFontStatic.


void CFontStatic::SetFontStyle(DWORD dwStyle)

This function is, in most of the cases, only used indirectly. It takes a DWORD as an argument. The different kind of styles are represented by defines that you can find in the section "The style defines".


void CFontStatic::SetBackground(DWORD dwBgColor)

This function sets the background color of the client rect. If this isn't used, the text's background will be transparent. You set the color by using the macro RGB. I.e., RGB(255,0,255).


void CFontStatic::SetFontStatic(CString szFont, 
             int nSize, DWORD dwColor, DWORD dwStyle)

This is the main function in the class and it is used to set the font.

  • CString szFont

    This is the font's name. I.e., "Arial".

  • int nSize

    This is the font's size in pixels.

  • DWORD dwColor

    The color of the text.

  • DWORD dwStyle

    The style of the text. See the section "The style defines" for information.

The style defines

There are different kinds of styles that can be used:


Using CFontStatic

Step 1:

Declare an instance of CStatic and locate the declaration in the .h file. Change the declaration from CStatic to CFontStatic. Don't forget to include FontStatic.h in your project.

Step 2:

Let's use the font "Arial", size 25, and set the color of the text to white. Also make the text italic, bold, and antialiased.

// Set the font to arial, size 25 and the color white.
// The text should be italic, bold and antialiased.
                       FS_ITALIC | FS_BOLD | FS_ANTIALIAS);
// Set the background of the rect to black

Or if you want to use the default font with no background color:

// The text should be bold and underlined.
m_example.SetFontStyle(FS_BOLD | FS_UNDERLINED);

Or if you want to use the font "MS Sans Serif" with the color red and center it in the rect:

// The text should now be centered and red.
m_example.SetFontStatic("MS Sans Serif",12, 

Or if... You get the point. Wink | ;)

Step 3:

Now you may want to change the text of the static control. This is done as usual with the SetWindowText function.

// Set the text in the CFontStatic
m_example.SetWindowText("Hello World!");


  • 2004-06-21

    Corrected some minor spelling-errors.

  • 2004-06-12

    Changed some minor errors and added an enlarged version of the screenshot.

  • 2004-06-11

    This article was written.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Patrik Svensson
Software Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey8-Feb-12 20:54
membermanoj kumar choubey8-Feb-12 20:54 
GeneralError Pin
Ankata8-Jul-08 0:09
memberAnkata8-Jul-08 0:09 
QuestionFlicker free version ? Pin
sdancer7527-Aug-07 0:34
membersdancer7527-Aug-07 0:34 
AnswerRe: Flicker free version ? Pin
Patrik Svensson27-Aug-07 1:53
memberPatrik Svensson27-Aug-07 1:53 
QuestionFont Instller ? [modified] Pin
Thanhnamdo24-Jul-06 17:31
memberThanhnamdo24-Jul-06 17:31 
GeneralLNK2019 Pin
ChriBa8-Feb-06 10:25
memberChriBa8-Feb-06 10:25 
GeneralNot working with CFormView Pin
navinkaus16-Aug-05 3:30
membernavinkaus16-Aug-05 3:30 
GeneralRe: Not working with CFormView Pin
Patrik Svensson16-Aug-05 10:28
memberPatrik Svensson16-Aug-05 10:28 
GeneralRe: Not working with CFormView Pin
navinkaus17-Aug-05 1:49
membernavinkaus17-Aug-05 1:49 
GeneralRe: Not working with CFormView Pin
softwarejunkie16-Oct-05 14:35
membersoftwarejunkie16-Oct-05 14:35 
GeneralNice custom CStatic control Pin
kongy7920-Jul-05 10:57
memberkongy7920-Jul-05 10:57 
GeneralOne Mistake! Pin
HoangLan16-Apr-05 17:34
memberHoangLan16-Apr-05 17:34 
GeneralRe: One Mistake! Pin
Steve Mayfield16-Apr-05 18:56
memberSteve Mayfield16-Apr-05 18:56 
GeneralHello Pin
Patrik Svensson21-Feb-05 3:56
memberPatrik Svensson21-Feb-05 3:56 
General#define Pin
Gigy20-Feb-05 20:30
sussGigy20-Feb-05 20:30 
GeneralRe: #define Pin
Gigy20-Feb-05 20:32
sussGigy20-Feb-05 20:32 
GeneralNew Version Pin
stevy822-Feb-05 1:53
memberstevy822-Feb-05 1:53 
GeneralAutosizing height Pin
Urban Olars17-Jan-05 11:00
memberUrban Olars17-Jan-05 11:00 
GeneralRe: Autosizing height Pin
tom.schultz30-May-05 21:21
membertom.schultz30-May-05 21:21 
Generalv good (SetWindowText) Pin
habibkassem1-Dec-04 11:48
memberhabibkassem1-Dec-04 11:48 
QuestionWhy create static control font in OnPaint()? Performance? Pin
Behzad Ebrahimi21-Nov-04 7:41
memberBehzad Ebrahimi21-Nov-04 7:41 
AnswerRe: Why create static control font in OnPaint()? Performance? Pin
Patrik Svensson25-Nov-04 5:36
memberPatrik Svensson25-Nov-04 5:36 
QuestionUpdate problem? Pin
Kiriko18-Nov-04 3:05
memberKiriko18-Nov-04 3:05 
AnswerRe: Update problem? Pin
Patrik Svensson20-Nov-04 3:38
memberPatrik Svensson20-Nov-04 3:38 
GeneralRe: Update problem? Pin
bart_Schepers14-May-08 4:56
memberbart_Schepers14-May-08 4:56 
QuestionIs there a win32 version? Pin
write2warriors23-Aug-04 17:36
memberwrite2warriors23-Aug-04 17:36 
AnswerRe: Is there a win32 version? Pin
Patrik Svensson20-Nov-04 3:42
memberPatrik Svensson20-Nov-04 3:42 
GeneralSetWindowText Pin
AndriesH18-Jul-04 20:55
memberAndriesH18-Jul-04 20:55 
GeneralRe: SetWindowText Pin
Patrik Svensson19-Jul-04 8:06
memberPatrik Svensson19-Jul-04 8:06 
GeneralRe: SetWindowText Pin
ColinDavies23-Jul-04 13:49
memberColinDavies23-Jul-04 13:49 
GeneralGood job! Pin
WREY13-Jun-04 4:59
memberWREY13-Jun-04 4:59 
GeneralRe: Good job! Pin
Patrik Svensson13-Jun-04 5:42
memberPatrik Svensson13-Jun-04 5:42 
GeneralRe: Good job! Pin
diilbert15-Jun-04 13:08
memberdiilbert15-Jun-04 13:08 
GeneralRe: Good job! Pin
Jodde16-Jun-04 20:39
memberJodde16-Jun-04 20:39 
Generalhm Pin
wb12-Jun-04 13:02
memberwb12-Jun-04 13:02 
GeneralRe: hm Pin
Patrik Svensson12-Jun-04 15:25
memberPatrik Svensson12-Jun-04 15:25 
GeneralRe: hm Pin
Johann Gerell14-Jun-04 2:15
memberJohann Gerell14-Jun-04 2:15 
GeneralRe: hm Pin
wb21-Jun-04 1:58
memberwb21-Jun-04 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 21 Jun 2004
Article Copyright 2004 by Patrik Svensson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid