一、在App头文件中加入:
BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam);
class TApplicationFont : public CObject {
private:
CString m_strFaceName;
int m_nFontSize;
int m_nPointSize;
CFont m_Font;
private:
void createFont(void);
public:
TApplicationFont(LPCTSTR szFaceName);
virtual ~TApplicationFont();
CFont *GetFont(void);
CString& GetFaceName(void);
int GetFontSize(void);
int GetPointSize(void);
void SetFaceName(LPCTSTR szFaceName);
};
二、在实现App的.cpp中加入:
//
// Global font
//
TApplicationFont g_Font(_T("楷体_GB2312"));
//
// Callback functions for EnumChildWindows
//
// lParam is a pointer to CFont object
BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam)
{
CFont *pFont = (CFont*)lparam;
CWnd *pWnd = CWnd::FromHandle(hwnd);
pWnd->SetFont(pFont);
return TRUE;
}
//=============================================================================
// TApplicationFont
//
TApplicationFont::TApplicationFont(LPCTSTR szFaceName)
{
m_strFaceName = szFaceName;
createFont();
}
TApplicationFont::~TApplicationFont()
{
}
void TApplicationFont::createFont(void)
{
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(metrics);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, 0);
CWindowDC wdc(NULL);
int nLPixY = GetDeviceCaps(wdc.m_hDC, LOGPIXELSY);
m_nFontSize = metrics.lfStatusFont.lfHeight;
//如果要改变字大小则可m_nFontSize = metrics.lfStatusFont.lfHeight = 36;
m_nPointSize = -MulDiv(m_nFontSize,72,nLPixY);
_tcscpy(metrics.lfStatusFont.lfFaceName, m_strFaceName);
m_Font.CreateFontIndirect(&metrics.lfStatusFont);
}
CFont *TApplicationFont::GetFont(void)
{
return &m_Font;
}
CString& TApplicationFont::GetFaceName(void)
{
return m_strFaceName;
}
int TApplicationFont::GetFontSize(void)
{
return m_nFontSize;
}
int TApplicationFont::GetPointSize(void)
{
return m_nPointSize;
}
void TApplicationFont::SetFaceName(LPCTSTR szFaceName)
{
m_strFaceName = szFaceName;
m_Font.DeleteObject();
createFont();
}
三、在OnCreate或者OnInitDialog中加入:
extern TApplicationFont g_Font;
// g_Font is an object of type CFont or derived from CFont
EnumChildWindows(m_hWnd, ::SetChildFont, &g_Font);