前提假定,對話框上有一個ID為IDC_TEST的控件。
在OnInitDialog函數(shù)中插入一下代碼:
m_font.CreatePointFont(909,_T("宋體"));
GetDlgItem(IDC_TEST)->SetFont(&m_font);
編譯后便可看見效果
說明:
在我第一次嘗試更改控件變量的字體時,使用了如下的代碼:
CFont font
font.CreatePointFont(909,_T("宋體"));
GetDlgItem(IDC_TEST)->SetFont(&font);

結(jié)果,控件的字體并沒有改變。后來得知SetFont函數(shù)有如下要求:
The SetFont() member function of the CWnd class changes the font in a specified control. For this function to work correctly in a Windows- based application, you must ensure that the CFont object specified in the SetFont() call is not destroyed until after the specified control has been destroyed. 其中的"...you must ensure that the CFont object specified in the SetFont() call is not destroyed until after the specified control has been destroyed. "便是關(guān)鍵。
所以在更該控件的字體時,CFont對象必須是靜態(tài)或者成員變量。
posted on 2009-12-30 22:05
zhaoyg 閱讀(521)
評論(0) 編輯 收藏 引用 所屬分類:
MFC學(xué)習(xí)筆記