随笔-59  评论-36  文章-0  trackbacks-0
前提假定,对话框上有一个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对象必须是静态或者成员变量。
posted on 2009-12-30 22:05 zhaoyg 阅读(496) 评论(0)  编辑 收藏 引用 所属分类: MFC学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理