Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

CFontStatic

Rate me:
Please Sign up or sign in to vote.
4.34/5 (22 votes)
20 Jun 2004CPOL2 min read 131.1K   5.4K   51   38
An easy way to change the look of a CStatic.

Sample Image - CFontStatic.gif

Introduction

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.

SetFontStyle

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".

SetBackground

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).

SetFontStatic

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:

  • FS_BOLD
  • FS_ITALIC
  • FS_UNDERLINED
  • FS_STRIKETHROUGH
  • FS_ANTIALIAS
  • FS_CENTER
  • FS_LEFT
  • FS_RIGHT

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.
m_example.SetFontStatic("Arial",25,RGB(255,255,255), 
                       FS_ITALIC | FS_BOLD | FS_ANTIALIAS);
// Set the background of the rect to black
m_example.SetBackground(RGB(0,0,0));

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, 
                         RGB(255,0,0),FS_CENTER)

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

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!");

History

  • 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.

License

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


Written By
Software Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey8-Feb-12 19:54
professionalManoj Kumar Choubey8-Feb-12 19:54 
GeneralError Pin
Ankata7-Jul-08 23:09
Ankata7-Jul-08 23:09 
QuestionFlicker free version ? Pin
sdancer7526-Aug-07 23:34
sdancer7526-Aug-07 23:34 
AnswerRe: Flicker free version ? Pin
Patrik Svensson27-Aug-07 0:53
Patrik Svensson27-Aug-07 0:53 
QuestionFont Instller ? [modified] Pin
Thanhnamdo24-Jul-06 16:31
Thanhnamdo24-Jul-06 16:31 
GeneralLNK2019 Pin
ChriBa8-Feb-06 9:25
ChriBa8-Feb-06 9:25 
GeneralNot working with CFormView Pin
navinkaus16-Aug-05 2:30
navinkaus16-Aug-05 2:30 
GeneralRe: Not working with CFormView Pin
Patrik Svensson16-Aug-05 9:28
Patrik Svensson16-Aug-05 9:28 
GeneralRe: Not working with CFormView Pin
navinkaus17-Aug-05 0:49
navinkaus17-Aug-05 0:49 
GeneralRe: Not working with CFormView Pin
softwarejunkie16-Oct-05 13:35
softwarejunkie16-Oct-05 13:35 
GeneralNice custom CStatic control Pin
kongy7920-Jul-05 9:57
kongy7920-Jul-05 9:57 
GeneralOne Mistake! Pin
HoangLan16-Apr-05 16:34
HoangLan16-Apr-05 16:34 
GeneralRe: One Mistake! Pin
Steve Mayfield16-Apr-05 17:56
Steve Mayfield16-Apr-05 17:56 
GeneralHello Pin
Patrik Svensson21-Feb-05 2:56
Patrik Svensson21-Feb-05 2:56 
General#define Pin
Gigy20-Feb-05 19:30
Gigy20-Feb-05 19:30 
GeneralRe: #define Pin
Gigy20-Feb-05 19:32
Gigy20-Feb-05 19:32 
GeneralNew Version Pin
stevy822-Feb-05 0:53
stevy822-Feb-05 0:53 
GeneralAutosizing height Pin
Urban Olars17-Jan-05 10:00
Urban Olars17-Jan-05 10:00 
GeneralRe: Autosizing height Pin
tom.schultz30-May-05 20:21
tom.schultz30-May-05 20:21 
Generalv good (SetWindowText) Pin
habibkassem1-Dec-04 10:48
habibkassem1-Dec-04 10:48 
QuestionWhy create static control font in OnPaint()? Performance? Pin
Behzad Ebrahimi21-Nov-04 6:41
Behzad Ebrahimi21-Nov-04 6:41 
AnswerRe: Why create static control font in OnPaint()? Performance? Pin
Patrik Svensson25-Nov-04 4:36
Patrik Svensson25-Nov-04 4:36 
Thank you for your feedback.
I will look into this right away and try to post something here on CodeProject as soon as i have time.

/ Patrik


"If knowledge can create problems, it is not through ignorance that we can solve them."
Isaac Asimov
QuestionUpdate problem? Pin
Kiriko18-Nov-04 2:05
Kiriko18-Nov-04 2:05 
AnswerRe: Update problem? Pin
Patrik Svensson20-Nov-04 2:38
Patrik Svensson20-Nov-04 2:38 
GeneralRe: Update problem? Pin
Blob_Dlg14-May-08 3:56
Blob_Dlg14-May-08 3:56 

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.