设置控件的字体

设置控件的字体
前提假定,对话框上有一个ID为IDC_TEST的控件。

在OnInitDialog函数中插入一下代码:
m_font.CreatePointFont( 909 ,_T( " 宋体 " ));
GetDlgItem(IDC_TEST)
-> SetFont( & m_font);
编译后便可看见效果

说明:
在我第一次尝试更改控件变量的字体时,使用了如下的代码:
CFont font
font.CreatePointFont(
909 ,_T( " 宋体 " ));
GetDlgItem(IDC_TEST)
-> SetFont( & font);

结果,控件的字体并没有改变。后来得知SetFont函数有如下要求:
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. "便是关键。
所以在更该控件的字体时,CFont对象必须是静态或者成员变量。

你可能感兴趣的:(设置控件的字体)