MFC中控件的默認字體是這樣的
在Windows上設置控件字體的辦法是向該控件發送WM_SETFONT消息,但是如果窗口上控件過多的話則需要手動去遍歷控件,麻煩了點,好在MFC提供了一個SendMessageToDescendants方法,它是CWnd的成員方法,它的功能就是向窗口的所有子控件發送消息。
修改字體的時機最好在窗口及其子控件初始化完成且還沒顯示的時候,也就是主窗口收到WM_CREATE消息時。MFC在OnCreate方法中處理即可
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (__super::OnCreate(lpCreateStruct) == -1)
return -1;
// 創建幾個控件測試
const auto text = new CStatic;
text->Create(_T("Static Text"), WS_CHILD | WS_VISIBLE, CRect(100, 10, 200, 30), this);
const auto button = new CButton;
button->Create(_T("Button"), WS_CHILD | WS_VISIBLE, CRect(10, 10, 80, 30), this, 0);
const auto edit = new CEdit;
edit->Create(WS_CHILD | WS_VISIBLE, CRect(10, 50, 80, 70), this, 0);
// 獲取系統默認字體
NONCLIENTMETRICS ncm{ 0 };
ncm.cbSize = sizeof(ncm);
::SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
// 創建字體
const auto hFont = ::CreateFontIndirectW(&ncm.lfMessageFont);
// 向所有子控件發送WM_SETFONT消息
SetFont(CFont::FromHandle(hFont));
SendMessageToDescendants(WM_SETFONT, (WPARAM)hFont);
return 0;
}
在中文操作系統上默認字體是微軟雅黑,效果如圖