CFont::FromHandle
返回指向 CFont 对象,同时使 HFONT 处理Windows GDI字体对象。
static CFont* PASCAL FromHandle(
HFONT hFont
);
参数
- hFont
对于Windows字体的一 HFONT 处理。
返回值
为 CFont 对象的指针,如果成功;否则 NULL。
备注
如果一 CFont 对象尚未附加到句柄,一个临时 CFont 对象创建并附加。 该临时 CFont 对象是有效的,直至,则下次应用程序具有空闲时间在其事件循环,此时,所有瞬态图形对象被删除。 另一个是在这是临时对象在处理有效的windows消息过程中。
示例
// The code fragment shows how to create a font object using
// Windows API CreateFontIndirect(), convert the HFONT to a
// CFont* before selecting the font object into a DC (device
// context) for text drawing, and finally delete the font object.
// Initialize a CFont object with the characteristics given
// in a LOGFONT structure.
LOGFONT lf;
// clear out structure
memset(&lf, 0, sizeof(LOGFONT));
// request a 12-pixel-height font
lf.lfHeight = 12;
// request a face name "Arial"
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE, _T("Arial"), 7);
// create the font
HFONT hfont = ::CreateFontIndirect(&lf);
// Convert the HFONT to CFont*.
CFont* pfont = CFont::FromHandle(hfont);
// Do something with the font just created...
CClientDC dc(this);
CFont* def_font = dc.SelectObject(pfont);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
::DeleteObject(hfont);
要求
Header: afxwin.h