CFont クラスCFont Class
Windows のグラフィック デバイス インターフェイス (GDI) のフォントをカプセル化したもので、フォントを操作するメンバー関数を提供します。Encapsulates a Windows graphics device interface (GDI) font and provides member functions for manipulating the font.
構文Syntax
class CFont : public CGdiObject
メンバーMembers
パブリック コンストラクターPublic Constructors
名前Name | 説明Description |
---|---|
CFont:: CFontCFont::CFont | CFont オブジェクトを構築します。Constructs a CFont object. |
パブリック メソッドPublic Methods
名前Name | 説明Description |
---|---|
CFont:: CreateFontCFont::CreateFont | 指定した特性を使用して、を初期化し CFont ます。Initializes a CFont with the specified characteristics. |
CFont:: CreateFontIndirectCFont::CreateFontIndirect | CFont 構造体で指定された特性を使用してオブジェクトを初期化 LOGFONT します。Initializes a CFont object with the characteristics given in a LOGFONT structure. |
CFont:: CreatePointFontCFont::CreatePointFont | 指定した高さを使用してを初期化し CFont ます。ポイントの1/10 として測定します。Initializes a CFont with the specified height, measured in tenths of a point, and typeface. |
CFont:: CreatePointFontIndirectCFont::CreatePointFontIndirect | と同じですが CreateFontIndirect 、フォントの高さは論理単位ではなく1/10 ポイントで測定される点が異なります。Same as CreateFontIndirect except that the font height is measured in tenths of a point rather than logical units. |
CFont:: FromHandleCFont::FromHandle | CFont Windows HFONT が指定されている場合に、オブジェクトへのポインターを返します。Returns a pointer to a CFont object when given a Windows HFONT. |
CFont:: GetLogFontCFont::GetLogFont | に、 LOGFONT オブジェクトにアタッチされている論理フォントに関する情報を格納 CFont します。Fills a LOGFONT with information about the logical font attached to the CFont object. |
パブリック演算子Public Operators
名前Name | 説明Description |
---|---|
CFont:: operator HFONTCFont::operator HFONT | オブジェクトにアタッチされている Windows GDI フォントハンドルを返し CFont ます。Returns the Windows GDI font handle attached to the CFont object. |
解説Remarks
オブジェクトを使用するには CFont
、 CFont
オブジェクトを構築し、 createfont、 createfontindirect、 Createpointfont、または Createpointfontindirectを使用してオブジェクトに Windows フォントをアタッチし、オブジェクトのメンバー関数を使用してフォントを操作します。To use a CFont
object, construct a CFont
object and attach a Windows font to it with CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect, and then use the object's member functions to manipulate the font.
CreatePointFont
および関数は、 CreatePointFontIndirect
CreateFont
CreateFontIndirect
ポイントサイズから論理単位に自動的にフォントの高さを変換するため、またはより簡単に使用できます。The CreatePointFont
and CreatePointFontIndirect
functions are often easier to use than CreateFont
or CreateFontIndirect
since they do the conversion for the height of the font from a point size to logical units automatically.
の詳細につい CFont
ては、「 グラフィックオブジェクト」を参照してください。For more information on CFont
, see Graphic Objects.
継承階層Inheritance Hierarchy
CFont
要件Requirements
ヘッダー: afxwin.hHeader: afxwin.h
CFont:: CFontCFont::CFont
CFont
オブジェクトを構築します。Constructs a CFont
object.
CFont();
解説Remarks
結果のオブジェクトは CreateFont
、使用する前に、、、またはで初期化する必要があり CreateFontIndirect
CreatePointFont
CreatePointFontIndirect
ます。The resulting object must be initialized with CreateFont
, CreateFontIndirect
, CreatePointFont
, or CreatePointFontIndirect
before it can be used.
例Example
CFont font;
CFont:: CreateFontCFont::CreateFont
指定し CFont
た特性を使用してオブジェクトを初期化します。Initializes a CFont
object with the specified characteristics.
BOOL CreateFont(
int nHeight,
int nWidth,
int nEscapement,
int nOrientation,
int nWeight,
BYTE bItalic,
BYTE bUnderline,
BYTE cStrikeOut,
BYTE nCharSet,
BYTE nOutPrecision,
BYTE nClipPrecision,
BYTE nQuality,
BYTE nPitchAndFamily,
LPCTSTR lpszFacename);
パラメーターParameters
nHeightnHeight
フォントの必要な高さ (論理単位) を指定します。Specifies the desired height (in logical units) of the font. lfHeight
説明については、Windows SDK のLOGFONT構造体のメンバーを参照してください。See the lfHeight
member of the LOGFONTstructure in the Windows SDK for a description. NHeight の絶対値は、変換後に16384デバイスユニットを超えることはできません。The absolute value of nHeight must not exceed 16,384 device units after it is converted. すべての高さの比較で、フォントマッパーは、要求されたサイズを超えない最大のフォント、またはすべてのフォントが要求されたサイズを超えた場合に最小のフォントを検索します。For all height comparisons, the font mapper looks for the largest font that does not exceed the requested size or the smallest font if all the fonts exceed the requested size.
nWidthnWidth
フォントの文字の平均幅 (論理単位) を指定します。Specifies the average width (in logical units) of characters in the font. NWidth が0の場合、デバイスの縦横比が、使用可能なフォントのデジタル化された縦横比と照合され、最も近い一致が検索されます。これは、差分の絶対値によって決まります。If nWidth is 0, the aspect ratio of the device will be matched against the digitization aspect ratio of the available fonts to find the closest match, which is determined by the absolute value of the difference.
nEscapementnEscapement
傾斜ベクターとディスプレイサーフェイスの x 軸の間の角度 (0.1 度単位) を指定します。Specifies the angle (in 0.1-degree units) between the escapement vector and the x-axis of the display surface. 傾斜ベクターは、行の最初と最後の文字の原点を通る線です。The escapement vector is the line through the origins of the first and last characters on a line. 角度は、x 軸から反時計回りに計測されます。The angle is measured counterclockwise from the x-axis. lfEscapement
LOGFONT
詳細については、Windows SDK の構造体のメンバーを参照してください。See the lfEscapement
member in the LOGFONT
structure in the Windows SDK for more information.
nOrientationnOrientation
文字のベースラインと x 軸の間の角度 (0.1 度単位) を指定します。Specifies the angle (in 0.1-degree units) between the baseline of a character and the x-axis. 角度は、y 方向が上にある座標系の y 方向が下向きで x 軸から時計回りになる座標系の x 軸から反時計回りに計測されます。The angle is measured counterclockwise from the x-axis for coordinate systems in which the y-direction is down and clockwise from the x-axis for coordinate systems in which the y-direction is up.
nWeightnWeight
フォントの太さ (1000 あたりインク付きピクセル単位) を指定します。Specifies the font weight (in inked pixels per 1000). LOGFONT
詳細については、Windows SDK の構造体の lfWeight メンバーを参照してください。See the lfWeight member in the LOGFONT
structure in the Windows SDK for more information. 説明されている値は概数です。実際の外観は、タイプフェイスによって異なります。The described values are approximate; the actual appearance depends on the typeface. 一部のフォントには、FW_NORMAL、FW_REGULAR、および FW_BOLD の重みしかありません。Some fonts have only FW_NORMAL, FW_REGULAR, and FW_BOLD weights. FW_DONTCARE が指定されている場合は、既定の重みが使用されます。If FW_DONTCARE is specified, a default weight is used.
bItalicbItalic
フォントを斜体にするかどうかを指定します。Specifies whether the font is italic.
bUnderlinebUnderline
フォントを下線付きにするかどうかを指定します。Specifies whether the font is underlined.
cStrikeOutcStrikeOut
フォントの文字を取り消してもよいかどうかを指定します。0以外の値に設定されている場合は、取り消し線のフォントを指定します。Specifies whether characters in the font are struck out. Specifies a strikeout font if set to a nonzero value.
nCharSetnCharSet
フォントの文字セットを指定し lfCharSet
LOGFONT
ます。値の一覧については、Windows SDK の構造体でメンバーを参照してください。Specifies the font's character setSee the lfCharSet
member in the LOGFONT
structure in the Windows SDK for a list of values.
OEM 文字セットはシステムに依存します。The OEM character set is system-dependent.
他の文字セットを含むフォントがシステムに存在する可能性があります。Fonts with other character sets may exist in the system. 文字セットが不明なフォントを使用するアプリケーションでは、そのフォントで表示される文字列の変換または解釈を試行しないでください。An application that uses a font with an unknown character set must not attempt to translate or interpret strings that are to be rendered with that font. 代わりに、文字列を出力デバイスドライバーに直接渡す必要があります。Instead, the strings should be passed directly to the output device driver.
フォントマッパーでは、DEFAULT_CHARSET 値は使用されません。The font mapper does not use the DEFAULT_CHARSET value. アプリケーションでこの値を使用して、フォントの名前とサイズが論理フォントを完全に記述できるようにすることができます。An application can use this value to allow the name and size of a font to fully describe the logical font. 指定した名前のフォントが存在しない場合は、任意の文字セットのフォントを指定したフォントに置き換えることができます。If a font with the specified name does not exist, a font from any character set can be substituted for the specified font. 予期しない結果を避けるために、アプリケーションでは DEFAULT_CHARSET 値を控えめに使用する必要があります。To avoid unexpected results, applications should use the DEFAULT_CHARSET value sparingly.
nOutPrecisionnOutPrecision
目的の出力精度を指定します。Specifies the desired output precision. 出力精度は、要求されたフォントの高さ、幅、文字の向き、傾斜、およびピッチとどの程度一致する必要があるかを定義します。The output precision defines how closely the output must match the requested font's height, width, character orientation, escapement, and pitch. lfOutPrecision
LOGFONT
値の一覧と詳細情報については、Windows SDK の構造体のメンバーを参照してください。See the lfOutPrecision
member in the LOGFONT
structure in the Windows SDK for a list of values and more information.
nClipPrecisionnClipPrecision
目的のクリッピング精度を指定します。Specifies the desired clipping precision. クリッピング精度は、クリッピング領域の外側にある文字をクリップする方法を定義します。The clipping precision defines how to clip characters that are partially outside the clipping region. lfClipPrecision
LOGFONT
値の一覧については、Windows SDK の構造体のメンバーを参照してください。See the lfClipPrecision
member in the LOGFONT
structure in the Windows SDK for a list of values.
埋め込み読み取り専用フォントを使用するには、アプリケーションで CLIP_ENCAPSULATE を指定する必要があります。To use an embedded read-only font, an application must specify CLIP_ENCAPSULATE.
デバイス、TrueType、ベクターフォントの一貫したローテーションを実現するために、アプリケーションでは、または演算子を使用して、CLIP_LH_ANGLES 値と他の任意の nClipPrecision 値を組み合わせることができます。To achieve consistent rotation of device, TrueType, and vector fonts, an application can use the OR operator to combine the CLIP_LH_ANGLES value with any of the other nClipPrecision values. CLIP_LH_ANGLES ビットが設定されている場合、すべてのフォントの回転は、座標系の向きが左ききか右ききかによって異なります。If the CLIP_LH_ANGLES bit is set, the rotation for all fonts depends on whether the orientation of the coordinate system is left-handed or right-handed. (座標系の向きの詳細については、 Norientation パラメーターの説明を参照してください)。CLIP_LH_ANGLES が設定されていない場合、デバイスフォントは常に反時計回りに回転しますが、他のフォントの回転は座標系の向きに依存します。(For more information about the orientation of coordinate systems, see the description of the nOrientation parameter.) If CLIP_LH_ANGLES is not set, device fonts always rotate counterclockwise, but the rotation of other fonts is dependent on the orientation of the coordinate system.
nQualitynQuality
フォントの出力品質を指定します。これにより、GDI が論理フォント属性を実際の物理フォントの属性と一致させるために必要な方法が定義されます。Specifies the font's output quality, which defines how carefully the GDI must attempt to match the logical-font attributes to those of an actual physical font. lfQuality
LOGFONT
値の一覧については、Windows SDK の構造体のメンバーを参照してください。See the lfQuality
member in the LOGFONT
structure in the Windows SDK for a list of values.
nPitchAndFamilynPitchAndFamily
フォントのピッチとファミリを指定します。Specifies the pitch and family of the font. lfPitchAndFamily
LOGFONT
値の一覧と詳細情報については、Windows SDK の構造体のメンバーを参照してください。See the lfPitchAndFamily
member in the LOGFONT
structure in the Windows SDK for a list of values and more information.
lpszFacenamelpszFacename
CString
フォントのタイプフェイス名を指定する、null で終わる文字列へのポインター。A CString
or pointer to a null-terminated string that specifies the typeface name of the font. この文字列の長さは30文字以下でなければなりません。The length of this string must not exceed 30 characters. Windows Enumfontfamilies 関数を使用すると、現在使用可能なすべてのフォントを列挙できます。The Windows EnumFontFamilies function can be used to enumerate all currently available fonts. Lpszfacename が NULL の場合、GDI はデバイスに依存しないタイプフェイスを使用します。If lpszFacename is NULL, the GDI uses a device-independent typeface.
戻り値Return Value
正常終了した場合は 0 以外を返します。それ以外の場合は 0 を返します。Nonzero if successful; otherwise 0.
解説Remarks
フォントは、その後、任意のデバイスコンテキストのフォントとして選択できます。The font can subsequently be selected as the font for any device context.
関数は、 CreateFont
新しい WINDOWS GDI フォントを作成しません。The CreateFont
function does not create a new Windows GDI font. GDI で使用可能な物理フォントから最も近い一致を選択するだけです。It merely selects the closest match from the physical fonts available to the GDI.
アプリケーションでは、論理フォントを作成するときにほとんどのパラメーターに既定の設定を使用できます。Applications can use the default settings for most parameters when creating a logical font. 常に特定の値を指定する必要があるパラメーターは、 nHeight と lpszfacename です。The parameters that should always be given specific values are nHeight and lpszFacename. NHeight と lpszfacename がアプリケーションによって設定されていない場合、作成される論理フォントはデバイスに依存します。If nHeight and lpszFacename are not set by the application, the logical font that is created is device-dependent.
CFont
関数によって作成されたオブジェクトが終了したら CreateFont
、を使用して CDC::SelectObject
デバイスコンテキストに別のフォントを選択し、不要になったオブジェクトを削除し CFont
ます。When you finish with the CFont
object created by the CreateFont
function, use CDC::SelectObject
to select a different font into the device context, then delete the CFont
object that is no longer needed.
例Example
// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.
// Initializes a CFont object with the specified characteristics.
CFont font;
VERIFY(font.CreateFont(
12, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial"))); // lpszFacename
// Do something with the font just created...
CClientDC dc(this);
CFont *def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font.DeleteObject();
CFont:: CreateFontIndirectCFont::CreateFontIndirect
CFont
LOGFONT構造体で指定された特性を使用してオブジェクトを初期化します。Initializes a CFont
object with the characteristics given in a LOGFONTstructure.
BOOL CreateFontIndirect(const LOGFONT* lpLogFont);
パラメーターParameters
lpLogFontlpLogFont
LOGFONT
論理フォントの特性を定義する構造体を指します。Points to a LOGFONT
structure that defines the characteristics of the logical font.
戻り値Return Value
正常終了した場合は 0 以外を返します。それ以外の場合は 0 を返します。Nonzero if successful; otherwise 0.
解説Remarks
フォントは、その後、任意のデバイスの現在のフォントとして選択できます。The font can subsequently be selected as the current font for any device.
このフォントには、 LOGFONT 構造体で指定されている特性があります。This font has the characteristics specified in the LOGFONT structure. CDC:: SelectObjectメンバー関数を使用してフォントを選択すると、GDI フォントマッパーは論理フォントと既存の物理フォントを一致させようとします。When the font is selected by using the CDC::SelectObject member function, the GDI font mapper attempts to match the logical font with an existing physical font. フォントマッパーが論理フォントと完全に一致するものを見つけることができない場合は、要求された特性の多くを可能な限り満たす代替フォントが提供されます。If the font mapper fails to find an exact match for the logical font, it provides an alternative font whose characteristics match as many of the requested characteristics as possible.
CFont
関数によって作成されたオブジェクトが不要になったら CreateFontIndirect
、を使用してデバイスコンテキストに別のフォントを選択し、不要になっ CDC::SelectObject
たオブジェクトを削除し CFont
ます。When you no longer need the CFont
object created by the CreateFontIndirect
function, use CDC::SelectObject
to select a different font into the device context, then delete the CFont
object that is no longer needed.
例Example
// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.
// Initializes a CFont object with the characteristics given
// in a LOGFONT structure.
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 12; // request a 12-pixel-height font
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE,
_T("Arial"), 7); // 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, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font.DeleteObject();
CFont:: CreatePointFontCFont::CreatePointFont
この関数は、指定されたタイプフェイスとポイントサイズのフォントを簡単に作成する方法を提供します。This function provides a simple way to create a font of a specified typeface and point size.
BOOL CreatePointFont(
int nPointSize,
LPCTSTR lpszFaceName,
CDC* pDC = NULL);
パラメーターParameters
nPointSizenPointSize
要求されたフォントの高さ (1/10 ポイント単位)。Requested font height in tenths of a point. (たとえば、12ポイントのフォントを要求するには、120を渡します)。(For instance, pass 120 to request a 12-point font.)
lpszFaceNamelpszFaceName
CString
フォントのタイプフェイス名を指定する、null で終わる文字列へのポインター。A CString
or pointer to a null-terminated string that specifies the typeface name of the font. この文字列の長さは30文字以下でなければなりません。The length of this string must not exceed 30 characters. Windows の EnumFontFamilies 関数を使用すると、現在使用可能なすべてのフォントを列挙できます。The Windows `EnumFontFamilies function can be used to enumerate all currently available fonts. Lpszfacename が NULL の場合、GDI はデバイスに依存しないタイプフェイスを使用します。If lpszFaceName is NULL, the GDI uses a device-independent typeface.
pDCpDC
NPointSize の高さを論理単位に変換するために使用される CDCオブジェクトへのポインター。Pointer to the CDC object to be used to convert the height in nPointSize to logical units. NULL の場合、変換には画面デバイスコンテキストが使用されます。If NULL, a screen device context is used for the conversion.
戻り値Return Value
成功した場合は0以外の。それ以外の場合は0。Nonzero if successful, otherwise 0.
解説Remarks
PDC によってポイントされている CDC オブジェクトを使用して、 nPointSize の高さを論理単位に自動的に変換します。It automatically converts the height in nPointSize to logical units using the CDC object pointed to by pDC.
CFont
関数によって作成されたオブジェクトが完成したら CreatePointFont
、まず、デバイスコンテキストからフォントを選択してから、オブジェクトを削除し CFont
ます。When you finish with the CFont
object created by the CreatePointFont
function, first select the font out of the device context, then delete the CFont
object.
例Example
// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.
CClientDC dc(this);
CFont font;
VERIFY(font.CreatePointFont(120, _T("Arial"), &dc));
// Do something with the font just created...
CFont *def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font.DeleteObject();
CFont:: CreatePointFontIndirectCFont::CreatePointFontIndirect
この関数は lfHeight
、のメンバーが LOGFONT
デバイス単位ではなく1/10 ポイントで解釈される点を除いて、createfontindirect と同じです。This function is the same as CreateFontIndirect except that the lfHeight
member of the LOGFONT
is interpreted in tenths of a point rather than device units.
BOOL CreatePointFontIndirect(
const LOGFONT* lpLogFont,
CDC* pDC = NULL);
パラメーターParameters
lpLogFontlpLogFont
論理フォントの特性を定義する LOGFONT 構造体を指します。Points to a LOGFONT structure that defines the characteristics of the logical font. lfHeight
構造体のメンバーは、 LOGFONT
論理単位ではなく、1/10 ポイントで測定されます。The lfHeight
member of the LOGFONT
structure is measured in tenths of a point rather than logical units. (たとえば、 lfHeight
12 ポイントのフォントを要求するには、を120に設定します)。(For instance, set lfHeight
to 120 to request a 12-point font.)
pDCpDC
の高さを論理単位に変換するために使用される CDC オブジェクトへのポインター lfHeight
。Pointer to the CDC object to be used to convert the height in lfHeight
to logical units. NULL の場合、変換には画面デバイスコンテキストが使用されます。If NULL, a screen device context is used for the conversion.
戻り値Return Value
成功した場合は0以外の。それ以外の場合は0。Nonzero if successful, otherwise 0.
解説Remarks
この関数 lfHeight
は、構造体を Windows に渡す前に pDC が指す CDC オブジェクトを使用して、の高さを論理単位に自動的に変換し LOGFONT
ます。This function automatically converts the height in lfHeight
to logical units using the CDC object pointed to by pDC before passing the LOGFONT
structure on to Windows.
CFont
関数によって作成されたオブジェクトが完成したら CreatePointFontIndirect
、まず、デバイスコンテキストからフォントを選択してから、オブジェクトを削除し CFont
ます。When you finish with the CFont
object created by the CreatePointFontIndirect
function, first select the font out of the device context, then delete the CFont
object.
例Example
// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.
LOGFONT lf;
// clear out structure.
memset(&lf, 0, sizeof(LOGFONT));
// request a 12-pixel-height font
lf.lfHeight = 120;
// request a face name "Arial".
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE, _T("Arial"), 7);
CClientDC dc(this);
CFont font;
VERIFY(font.CreatePointFontIndirect(&lf, &dc));
// Do something with the font just created...
CFont *def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font.DeleteObject();
CFont:: FromHandleCFont::FromHandle
CFont
WINDOWS GDI フォントオブジェクトの HFONT ハンドルが指定された場合に、オブジェクトへのポインターを返します。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
hFonthFont
Windows フォントを扱う HFONT ハンドル。An HFONT handle to a Windows font.
戻り値Return Value
成功した場合はオブジェクトへのポインター、 CFont
それ以外の場合は NULL。A pointer to a CFont
object if successful; otherwise NULL.
解説Remarks
CFont
オブジェクトがハンドルにまだアタッチされていない場合は、一時 CFont
オブジェクトが作成され、アタッチされます。If a CFont
object is not already attached to the handle, a temporary CFont
object is created and attached. この一時 CFont
オブジェクトは、アプリケーションが次にそのイベントループ内でアイドル状態になったときにのみ有効です。その時点で、すべての一時グラフィックオブジェクトが削除されます。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. これを言うもう1つの方法は、一時オブジェクトが、1つのウィンドウメッセージの処理中にのみ有効であることです。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);
CFont:: GetLogFontCFont::GetLogFont
の構造体のコピーを取得するには、この関数を呼び出し LOGFONT
CFont
ます。Call this function to retrieve a copy of the LOGFONT
structure for CFont
.
int GetLogFont(LOGFONT* pLogFont);
パラメーターParameters
pLogFontpLogFont
フォント情報を受け取る LOGFONT 構造体へのポインター。Pointer to the LOGFONT structure to receive the font information.
戻り値Return Value
関数が成功した場合は0以外。それ以外の場合は0。Nonzero if the function succeeds, otherwise 0.
例Example
// The code fragment shows how to retrieve a copy of the
// LOGFONT structure for a currently selected font of a window.
CFont *pFont = pWnd->GetFont();
if (NULL != pFont)
{
LOGFONT lf;
pFont->GetLogFont(&lf);
TRACE(_T("Typeface name of font = %s\n"), lf.lfFaceName);
}
CFont:: operator HFONTCFont::operator HFONT
オブジェクトにアタッチされているフォントの Windows GDI ハンドルを取得するには、この演算子を使用し CFont
ます。Use this operator to get the Windows GDI handle of the font attached to the CFont
object.
operator HFONT() const;
戻り値Return Value
成功した場合は、にアタッチされた Windows GDI フォントオブジェクトのハンドル CFont
。それ以外の場合は NULL。The handle of the Windows GDI font object attached to CFont
if successful; otherwise NULL.
解説Remarks
この演算子は、からフォントやテキストへの変換に自動的に使用されるため CFont
、 CFont
hfonts を想定している関数にオブジェクトを渡すことができます。Since this operator is automatically used for conversions from CFont
to Fonts and Text, you can pass CFont
objects to functions that expect HFONTs.
グラフィックオブジェクトの使用方法の詳細については、「Windows SDK の グラフィックオブジェクト 」を参照してください。For more information about using graphic objects, see Graphic Objects in the Windows SDK.
例Example
// The code fragment shows the usage of CFont::operator HFONT.
// 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);
CFont font1;
font1.CreateFontIndirect(&lf); // create the font
// CFont::operator HFONT automatically converts font1 from
// CFont* to HFONT.
CFont *font2 = CFont::FromHandle(font1);
// Do something with the font just created...
CClientDC dc(this);
CFont *def_font = dc.SelectObject(font2);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
// Done with the font. Delete the font object.
font1.DeleteObject();
関連項目See also
MFC サンプル HIERSVRMFC Sample HIERSVR
CGdiObject クラスCGdiObject Class
階層図Hierarchy Chart