Q: How do I change the font of a control?
A: The following is an example how to change the font for a static control...the example is a dialog-based application which does not matter regarding the setting of the font itself...
Code:
// Dialog.hpp
class CYourDlg : public CDialog
{
public:
~CYourDlg() { m_Font.DeleteObject(); }
...
private:
CFont m_Font;
};
// Dialog.cpp
BOOL CYourDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Creates a 12-point-Courier-font
m_Font.CreatePointFont(120, _T("Courier"));
// With a member variable associated to the static control
m_MyStatic.SetFont(&m_Font);
// Without a member variable
GetDlgItem(IDC_MY_STATIC)->SetFont(&m_Font);
}
The font itself can be created in many ways...the only important thing is that the font object itself exists as long as the control...in other words....keep it as a member of the control or surrounding dialog....
Code:
// First way
CFont Font;
Font.CreateFont(12, // Height
0, // Width
0, // Escapement
0, // Orientation
FW_BOLD, // Weight
FALSE, // Italic
TRUE, // Underline
0, // StrikeOut
ANSI_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
DEFAULT_QUALITY, // Quality
DEFAULT_PITCH | FF_SWISS, // PitchAndFamily
"Arial")); // Facename
// Second way
CFont Font;
LOGFONT lfLogFont;
memset(&lfLogFont, 0, sizeof(lfLogFont));
lfLogFont.lfHeight = 12; // 12-pixel-height
lfLogFont.lfWeight = FW_BOLD; // Bold
lfLogFont.lfUnderline = TRUE; // Underlined
strcpy(lfLogFont.lfFaceName, "Arial"); // Arial
Font.CreateFontIndirect(&lfLogFont);
// Third way
CFont Font;
LOGFONT lfLogFont;
memset(&lfLogFont, 0, sizeof(lfLogFont));
lfLogFont.lfHeight = 120; // 12-pixel-height
lfLogFont.lfWeight = FW_BOLD; // Bold
lfLogFont.lfUnderline = TRUE; // Underlined
strcpy(lfLogFont.lfFaceName, "Arial"); // Arial
Font.CreatePointFontIndirect(&lfLogFont);