共用方式為


CFont 類別

封裝 Windows 繪圖裝置介面 (GDI) 字型並提供操作字型的成員函式。

語法

class CFont : public CGdiObject

成員

公用建構函式

名稱 描述
CFont::CFont 建構 CFont 物件。

公用方法

名稱 描述
CFont::CreateFont CFont使用指定的特性初始化 。
CFont::CreateFontIndirect CFont使用 結構中指定的 LOGFONT 特性,初始化 物件。
CFont::CreatePointFont CFont使用指定的高度、以點數十分之為單位和字樣來初始化 。
CFont::CreatePointFontIndirect CreateFontIndirect 相同,不同之處在于字型高度是以點數的十分之一而不是邏輯單位來測量。
CFont::FromHandle 當指定 Windows HFONT 時, CFont 傳回 物件的指標。
CFont::GetLogFont 填入 LOGFONT ,其中包含附加至 CFont 物件的邏輯字型相關資訊。

公用運算子

名稱 描述
CFont::operator HFONT 傳回附加至 CFont 物件的 Windows GDI 字型控制碼。

備註

若要使用 CFont 物件,請建構 CFont 物件,並使用 、 CreateFontIndirectCreatePointFontCreatePointFontIndirect 將 Windows 字型附加至該 CreateFont 物件,然後使用物件的成員函式來操作字型。

CreatePointFontIndirectCreatePointFont 式通常比 CreateFontCreateFontIndirect 更容易使用,因為它們會自動將字型高度從點大小轉換成邏輯單元。

如需 的詳細資訊 CFont ,請參閱 繪圖物件

繼承階層架構

CObject

CGdiObject

CFont

需求

標頭:afxwin.h

CFont::CFont

建構 CFont 物件。

CFont();

備註

產生的物件必須先使用 CreateFont 、、 CreateFontIndirectCreatePointFontCreatePointFontIndirect 初始化,才能使用。

範例

CFont font;

CFont::CreateFont

CFont初始化具有指定特性的物件。

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

參數

nHeight
指定字型所需的高度(以邏輯單位為單位)。 lfHeight如需描述, LOGFONT 請參閱 Windows SDK 中的 結構成員。 的絕對值 nHeight 在轉換後不得超過 16,384 個裝置單位。 對於所有高度比較,字型對應程式會尋找最大字型,如果所有字型超過要求的大小,則為未超過要求的大小或最小的字型。

nWidth
指定字型中字元的平均寬度(以邏輯單位為單位)。 如果 nWidth 為 0,裝置的外觀比例將會與可用字型的數位化外觀比例進行比對,以找出最接近的相符專案,這是由差異的絕對值所決定。

nEscapement
指定逸出器向量與顯示表面 X 軸之間的角度(以 0.1 度為單位)。 逸出向量是一行中第一個字元和最後一個字元的原點的行。 角度會從 X 軸逆時針測量。 lfEscapement如需詳細資訊,請參閱 Windows SDK 結構中的 LOGFONT 成員。

nOrientation
指定字元的基準與 X 軸之間的角度(以 0.1 度為單位)。 角度會從座標系統的 X 軸逆時針測量,其中 y 方向為向下,而從 Y 方向向上的座標系統的 X 軸順時針測量角度。

nWeight
指定字型粗細(以每 1000 個筆跡圖元為單位)。 lfWeight如需詳細資訊,請參閱 Windows SDK 結構中的 LOGFONT 成員。 描述的值是近似值;實際外觀取決於字樣。 某些字型只有 FW_NORMALFW_REGULARFW_BOLD 粗細。 如果 FW_DONTCARE 已指定,則會使用預設權數。

bItalic
指定字型是否為斜體。

bUnderline
指定字型是否加上底線。

cStrikeOut
指定字型中的字元是否被擊出。如果設定為非零值,則指定刪除線字型。

nCharSet
指定字型的字元集查看 lfCharSet Windows SDK 中結構中的 LOGFONT 成員以取得值清單。

OEM 字元集是系統相依的。

具有其他字元集的字型可能存在於系統中。 使用具有未知字元集之字型的應用程式不得嘗試轉譯或解譯要使用該字型轉譯的字串。 相反地,字串應該直接傳遞至輸出裝置磁碟機。

字型對應程式不會使用 DEFAULT_CHARSET 值。 應用程式可以使用此值來允許字型的名稱和大小,以完整描述邏輯字型。 如果具有指定名稱的字型不存在,則任何字元集的字型都可以取代指定的字型。 為了避免非預期的結果,應用程式應該謹慎使用 DEFAULT_CHARSET 值。

nOutPrecision
指定所需的輸出有效位數。 輸出精確度定義輸出必須符合所要求字型的高度、寬度、字元方向、逸出器和音調的程度。 lfOutPrecision如需值清單和詳細資訊,請參閱 Windows SDK 中 結構中的 LOGFONT 成員。

nClipPrecision
指定所需的裁剪精確度。 裁剪精確度定義如何裁剪部分超出裁剪區域的字元。 lfClipPrecision如需值清單,請參閱 Windows SDK 結構中的 LOGFONT 成員。

若要使用內嵌唯讀字型,應用程式必須指定 CLIP_ENCAPSULATE

為了達到裝置、TrueType 和向量字型的一致旋轉,應用程式可以使用位 OR 運算子 ( | ) 將值與任何其他 nClipPrecision 值結合 CLIP_LH_ANGLESCLIP_LH_ANGLES如果設定位,則所有字型的旋轉取決於座標系統的方向是左手還是右手。 (如需座標系統方向的詳細資訊,請參閱 參數的描述 nOrientation 。如果未 CLIP_LH_ANGLES 設定,裝置字型一律會逆時針旋轉,但其他字型的旋轉取決於座標系統的方向。

nQuality
指定字型的輸出品質,其定義 GDI 必須嘗試將邏輯字型屬性與實際實體字型屬性相符的方式。 lfQuality如需值清單,請參閱 Windows SDK 結構中的 LOGFONT 成員。

nPitchAndFamily
指定字型的音調和系列。 lfPitchAndFamily如需值清單和詳細資訊,請參閱 Windows SDK 中 結構中的 LOGFONT 成員。

lpszFacename
指定 CString 字型名稱之 Null 終止字串的 或 指標。 此字串的長度不得超過 30 個字元。 Windows EnumFontFamilies 函式可用來列舉所有目前可用的字型。 如果 lpszFacenameNULL ,GDI 會使用裝置無關的字樣。

傳回值

如果成功則為非零;否則為 0。

備註

之後可以將字型選取為任何裝置內容的字型。

函式 CreateFont 不會建立新的 Windows GDI 字型。 它只會從 GDI 可用的實體字型中選取最接近的相符專案。

建立邏輯字型時,應用程式可以使用大部分參數的預設設定。 應該一律指定特定值的參數是 nHeightlpszFacename 。 如果 nHeight 應用程式未設定 和 lpszFacename ,則所建立的邏輯字型會與裝置相關。

當您完成 CFont 函式所 CreateFont 建立的物件時,請使用 CDC::SelectObject 來選取裝置內容中的不同字型,然後刪除 CFont 不再需要的物件。

範例

// 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::CreateFontIndirect

CFont使用 結構中指定的 LOGFONT 特性,初始化 物件。

BOOL CreateFontIndirect(const LOGFONT* lpLogFont);

參數

lpLogFont
指向 LOGFONT 定義邏輯字型特性的結構。

傳回值

如果成功則為非零;否則為 0。

備註

之後可以將字型選取為任何裝置的目前字型。

此字型具有 結構中指定的 LOGFONT 特性。 使用 CDC::SelectObject 成員函式選取字型時,GDI 字型對應程式會嘗試比對邏輯字型與現有的實體字型。 如果字型對應程式找不到邏輯字型的完全相符專案,它就會提供替代字型,其特性會盡可能符合許多要求的特性。

當您不再需要 CFont 函式所 CreateFontIndirect 建立的物件時,請使用 CDC::SelectObject 來選取裝置內容中的不同字型,然後刪除 CFont 不再需要的物件。

範例

// 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::CreatePointFont

此函式提供簡單的方法來建立指定字樣和點號的字型。

BOOL CreatePointFont(
    int nPointSize,
    LPCTSTR lpszFaceName,
    CDC* pDC = NULL);

參數

nPointSize
要求的字型高度在點的十分之一。 (例如,傳遞 120 以要求 12 點字型。

lpszFaceName
指定 CString 字型名稱之 Null 終止字串的 或 指標。 此字串的長度不得超過 30 個字元。 Windows EnumFontFamilies 函式可用來列舉所有目前可用的字型。 如果 lpszFaceNameNULL ,GDI 會使用裝置無關的字樣。

pDC
CDC要用來將高度 nPointSize 轉換成邏輯單元的物件指標。 如果 NULL 為 ,則會使用螢幕裝置內容進行轉換。

傳回值

如果成功,則為非零,否則為 0。

備註

它會使用 所 pDC 指向的 CDC 物件,自動將 高度 nPointSize 轉換成邏輯單元。

當您完成 CFont 函式所 CreatePointFont 建立的物件時,請先從裝置內容中選取字型,然後刪除 CFont 物件。

範例

// 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::CreatePointFontIndirect

此函式與 CreateFontIndirect 相同, lfHeightLOGFONT 不同之處在于 的成員會在點的十分之一而非裝置單位中解譯。

BOOL CreatePointFontIndirect(
    const LOGFONT* lpLogFont,
    CDC* pDC = NULL);

參數

lpLogFont
指向 LOGFONT 定義邏輯字型特性的結構。 結構 lfHeight 的成員 LOGFONT 是以點的十分之一而不是邏輯單位來測量。 (例如,設定 lfHeight 為 120 以要求 12 點字型。

pDC
CDC要用來將高度 lfHeight 轉換成邏輯單元的物件指標。 如果 NULL 為 ,則會使用螢幕裝置內容進行轉換。

傳回值

如果成功,則為非零,否則為 0。

備註

此函式會在將 結構傳遞 LOGFONT 至 Windows 之前,使用 CDCpDC 指向的物件,自動將 高度 lfHeight 轉換成邏輯單元。

當您完成 CFont 函式所 CreatePointFontIndirect 建立的物件時,請先從裝置內容中選取字型,然後刪除 CFont 物件。

範例

// 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::FromHandle

當指定 HFONT Windows GDI 字型物件的控制碼時,傳回物件的指標 CFont

static CFont* PASCAL FromHandle(HFONT hFont);

參數

hFont
HFONTWindows 字型的控制碼。

傳回值

如果成功, CFont 則為 物件的指標,否則 NULL 為 。

備註

CFont如果物件尚未附加至控制碼,則會建立並附加暫存 CFont 物件。 這個暫存 CFont 物件只有在下次應用程式在其事件迴圈中有閒置時間為止才有效,此時會刪除所有暫存繪圖物件。 另一種方法是,暫存物件只有在處理一個視窗訊息時才有效。

範例

// 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::GetLogFont

呼叫此函式以擷取 結構的 CFont 複本 LOGFONT

int GetLogFont(LOGFONT* pLogFont);

參數

pLogFont
LOGFONT要接收字型資訊的 結構指標。

傳回值

如果函式成功,則為非零,否則為 0。

範例

// 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 HFONT

使用此運算子可取得附加至 CFont 物件之字型的 Windows GDI 控制碼。

operator HFONT() const;

傳回值

如果成功,附加至 CFont 的 Windows GDI 字型物件的控制碼,則為 ,否則 NULL 為 。

備註

由於此運算子會自動用於從 CFont 轉換成 Fonts 和 Text 的轉換,因此您可以將物件傳遞 CFont 至預期的 HFONT 函式。

如需使用繪圖物件的詳細資訊,請參閱 Windows SDK 中的繪圖物件

範例

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

另請參閱

MFC 範例 HIERSVR
CGdiObject
階層架構圖表