次の方法で共有


CFont::FromHandle

HFONT ハンドルを Windows GDI フォントのオブジェクトに指定されている場合、CFont オブジェクトへのポインターを返します。

static CFont* PASCAL FromHandle(
   HFONT hFont 
);

パラメーター

  • hFont
    Windows フォントへの HFONT のハンドル。

戻り値

正常終了した場合はオブジェクトへのポインター CFont ; それ null

解説

ハンドルへの CFont のオブジェクトが既にアタッチされていない場合、CFont の一時的なオブジェクトが生成されて結び付けられます。CFont のこの一時オブジェクトはすべての一時的なグラフィカル オブジェクトが削除されます時点で、アプリケーションがイベント ループのアイドル時間を確保したときにのみまで有効です。これを指定するもう一つの方法は、一時オブジェクトは 1 種類のウィンドウ メッセージの処理中にのみ有効になります。

使用例

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

必要条件

ヘッダー: afxwin.h

参照

関連項目

CFont クラス

階層図