在一本VC的书中有这样的一个例子:
首先实例化一个字体对话框CFontDialog的对象FtDlg,再建立一个CFont类型的变量m_font,然后在菜单项的命令响应函数中加入代码:
m_font.CreateFontIndirect(FtDlg.m_cf.lpLogFont);
m_strFontName=FtDlg.m_cf.lpLogFont->lfFaceName;
Invalidate();
最后在View类的OnDraw()函数中使用选择的字体类型和大小输出字体的名称,当重复点击菜单项时出现非法操作,书上的解释是CFont的m_font对象已经关联了一个字体资源,所以当再次进入这个命令响应函数时会发生非法操作,我对于这个解释很不理解,为什么说CFont对象关联了一个字体资源?这句话是什么意思???
When you finish with the CFont object created by the CreateFontIndirect function, first select the font out of the device context, then delete the CFont object.
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 12; // request a 12-pixel-height font
strcpy(lf.lfFaceName, "Arial"); // request a face name "Arial"
VERIFY(font.CreateFontIndirect(&lf)); // create the font
// Do something with the font just created...
CClientDC dc(this);
CFont* def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, "Hello", 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font.DeleteObject();