CWnd::SetFont void SetFont( CFont* pFont, BOOL bRedraw = TRUE );ParameterspFontSpecifies the new font.bRedrawIf TRUE, redraw the CWnd object.ResSets the window’s current font to the specified font. If bRedraw is TRUE, the window will also be redrawn.
m_static.fontsize = 24;
void SetFont( CFont* pFont, BOOL bRedraw = TRUE );ParameterspFontSpecifies the new font.bRedrawIf TRUE, redraw the CWnd object.ResSets the window’s current font to the specified font. If bRedraw is TRUE, the window will also be redrawn.
CFont font;
font.CreatePointFont(fontsize*10,fontname);
m_static.SelectObject(&font);
CString s;
m_static.GetWindowText(s);
m_static.SetWindowText(s);
m_Static.SetFont(&m_font);
class CMyStatic : public CStatic
{
// Construction
public:
CMyStatic();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyStatic)
//}}AFX_VIRTUAL// Implementation
public:
BOOL RegisterWindowClass();
virtual ~CMyStatic(); // Generated message map functions
protected:
//{{AFX_MSG(CMyStatic)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
private:
CFont m_Font;
};MyStatic.cpp:
CMyStatic::CMyStatic()
{
RegisterWindowClass();
}CMyStatic::~CMyStatic()
{
}
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
//{{AFX_MSG_MAP(CMyStatic)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyStatic message handlersint CMyStatic::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
VERIFY(m_Font.CreatePointFont(100, "宋体" )); SetFont( &m_Font ); return 0;
}BOOL CMyStatic::RegisterWindowClass()
{
WNDCLASS wndcls; HINSTANCE hInstance; hInstance = AfxGetInstanceHandle(); if ( !GetClassInfo( hInstance, _T( "CMyStatic" ), &wndcls ) )
{
GetClassInfo( hInstance, _T( "STATIC" ), &wndcls );
wndcls.lpszClassName = _T( "CMyStatic" ); if (!AfxRegisterClass(&wndcls)) {
return FALSE;
}
} return TRUE;
}够详细了吧?
重点是——m_Font的生命周期应该不小于Static的生命周期,否则设置的字体就失效了,所以要将m_Font定义为Static的成员变量。当然你也可以定义为全局变量。