다음을 통해 공유


CFont::FromHandle

Returns a pointer to a CFont object when given an HFONT handle to a Windows GDI font object.

static CFont* PASCAL FromHandle( 
   HFONT hFont  
);

Parameters

  • hFont
    An HFONT handle to a Windows font.

Return Value

A pointer to a CFont object if successful; otherwise NULL.

Remarks

If a CFont object is not already attached to the handle, a temporary CFont object is created and attached. This temporary CFont object is valid only until the next time the application has idle time in its event loop, at which time all temporary graphic objects are deleted. Another way of saying this is that the temporary object is valid only during the processing of one window message.

Example

// 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);

Requirements

Header: afxwin.h

See Also

Reference

CFont Class

Hierarchy Chart