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.
About Patrik Svensson
![]() |
My name is Patrik and I'm 24 years old. Currently i'm studying at Linköping University (http://www.liu.se) in Sweden to become a civil engineer in computer technology.
Click here to view Patrik Svensson's online profile. |
'Windows > MFC' 카테고리의 다른 글
하위 객체의 멤버 변수 가져오기 (0) | 2013.10.02 |
---|---|
_T Macro는 어떤 경우에 사용하는 것인지? (0) | 2013.10.02 |
OnPaint 와 OnDraw의 차이점? (0) | 2013.10.02 |
색변경 폰트 변경 (0) | 2013.10.02 |
CBitmap 비트맵 보이기 (showImage) (0) | 2013.10.02 |