共用方式為


CFontDialog 類別

可讓您將字型選取對話方塊併入您的應用程式。

語法

class CFontDialog : public CCommonDialog

成員

公用建構函式

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

公用方法

名稱 描述
CFontDialog::D oModal 顯示對話方塊,並允許使用者進行選取。
CFontDialog::GetCharFormat 擷取所選字型的字元格式設定。
CFontDialog::GetColor 傳回所選字型的色彩。
CFontDialog::GetCurrentFont 將目前選取字型的特性指派給 LOGFONT 結構。
CFontDialog::GetFaceName 傳回所選字型的臉部名稱。
CFontDialog::GetSize 傳回所選取字型的點大小。
CFontDialog::GetStyleName 傳回所選字型的樣式名稱。
CFontDialog::GetWeight 傳回所選字型的粗細。
CFontDialog::IsBold 判斷字型是否為粗體。
CFontDialog::IsItalic 判斷字型是否為斜體。
CFontDialog::IsStrikeOut 判斷字型是否以刪除線顯示。
CFontDialog::IsUnderline 判斷字型是否加上底線。

公用資料成員

名稱 描述
CFontDialog::m_cf 用來自訂 CFontDialog 物件的 結構。

備註

物件 CFontDialog 是一個對話方塊,其中包含目前安裝在系統中的字型清單。 使用者可以從清單中選取特定字型,然後這個選取範圍會回報給應用程式。

若要建構 CFontDialog 物件,請使用提供的建構函式或衍生新的子類別,並使用您自己的自訂建構函式。

CFontDialog建構物件之後,您可以使用 m_cf 結構來初始化對話方塊中控制項的值或狀態。 m_cf 結構的類型為 CHOOSEFONT 。 如需此結構的詳細資訊,請參閱 Windows SDK。

初始化對話方塊物件的控制項之後,請呼叫 DoModal 成員函式以顯示對話方塊,並允許使用者選取字型。 DoModal 會傳回使用者選取 [確定] 或 [取消] 或 [IDCANCEL] 按鈕。

如果 DoModal 傳回 IDOK,您可以使用其中 CFontDialog 一個 成員函式來擷取使用者輸入的資訊。

您可以使用 Windows CommDlgExtendedError 函式來判斷對話方塊初始化期間是否發生錯誤,並深入瞭解錯誤。 如需此函式的詳細資訊,請參閱 Windows SDK。

CFontDialog 依賴隨附于 Windows 3.1 版和更新版本的 COMMDLG.DLL 檔案。

若要自訂對話方塊,請從 CFontDialog 衍生類別、提供自訂對話方塊範本,以及新增訊息對應來處理擴充控制項中的通知訊息。 任何未處理的訊息都應該傳遞至基類。

不需要自訂勾點函式。

如需使用 CFontDialog 的詳細資訊,請參閱 通用對話方塊類別

繼承階層架構

CObject

CCmdTarget

CWnd

CDialog

CCommonDialog

CFontDialog

需求

標頭: afxdlgs.h

CFontDialog::CFontDialog

建構 CFontDialog 物件。

CFontDialog(
    LPLOGFONT lplfInitial = NULL,
    DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,
    CDC* pdcPrinter = NULL,
    CWnd* pParentWnd = NULL);

CFontDialog(
    const CHARFORMAT& charformat,
    DWORD dwFlags = CF_SCREENFONTS,
    CDC* pdcPrinter = NULL,
    CWnd* pParentWnd = NULL);

參數

plfInitial
LOGFONT 資料結構的指標 ,可讓您設定某些字型的特性。

charFormat
CHARFORMAT 資料結構的指標 ,可讓您在豐富的編輯控制項中設定某些字型特性。

dwFlags
指定一或數個 choose-font 旗標。 可以使用位元 OR 運算子來合併一或數個預先設定的值。 如果您修改 m_cf.Flag 結構成員,請務必在您的變更中使用位元 OR 運算子,讓預設行為保持不變。 如需這些旗標的詳細資訊,請參閱 Windows SDK 中 CHOOSEFONT 結構的描述

pdcPrinter
printer-device 內容的指標。 若有提供,此參數會指向要選取字型之印表機的 printer-device 內容。

pParentWnd
字型對話方塊的父系或擁有者視窗的指標。

備註

請注意,建構函式會自動填入 CHOOSEFONT 結構的成員中。 您應該只在想要使用不同於預設值的字型對話方塊時,才變更這些設定。

注意

唯有在沒有 Rich Edit 控制項支援的情況下,此函式的第一版才會存在。

範例

// Show the font dialog with all the default settings.
CFontDialog dlg;
dlg.DoModal();

// Show the font dialog with 12 point "Times New Roman" as the
// selected font.
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));

CClientDC dc(this); // expects a CWnd that has already been initialized
lf.lfHeight = -MulDiv(12, dc.GetDeviceCaps(LOGPIXELSY), 72);
_tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("Times New Roman"));

CFontDialog fdlg(&lf);
fdlg.DoModal();

CFontDialog::D oModal

呼叫此函式以顯示 Windows 通用字型對話方塊,並允許使用者選擇字型。

virtual INT_PTR DoModal();

傳回值

IDOK 或 IDCANCEL。 如果傳回 IDCANCEL,請呼叫 Windows CommDlgExtendedError 函式來判斷是否發生錯誤。

IDOK 和 IDCANCEL 是常數,指出使用者是否選取 [確定] 或 [取消] 按鈕。

備註

如果您想要藉由設定m_cf 結構的成員 來初始化各種字型對話方塊控制項,您應該先執行此動作,再呼叫 DoModal ,但在建構對話方塊物件之後。

如果 DoModal 傳回 IDOK,您可以呼叫其他成員函式,將使用者的設定或資訊輸入擷取到對話方塊中。

範例

請參閱 CFontDialog::CFontDialog 和 CFontDialog ::GetColor 的範例 。

CFontDialog::GetCharFormat

擷取所選字型的字元格式設定。

void GetCharFormat(CHARFORMAT& cf) const;

參數

Cf
CHARFORMAT 結構,其中包含所選取字型字元格式的相關資訊。

CFontDialog::GetColor

呼叫此函式以擷取選取的字型色彩。

COLORREF GetColor() const;

傳回值

選取字型的色彩。

範例

// Get the color of the selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   COLORREF color = dlg.GetColor();
   TRACE(_T("Color of the selected font = %8x\n"), color);
}

CFontDialog::GetCurrentFont

呼叫此函式,將目前選取字型的特性指派給 LOGFONT 結構的成員

void GetCurrentFont(LPLOGFONT lplf);

參數

lplf
結構的指標 LOGFONT

備註

提供其他 CFontDialog 成員函式來存取目前字型的個別特性。

如果在呼叫 DoModal 期間呼叫 此函式,則會傳回當時的目前選取範圍(使用者在對話方塊中看到或已變更的內容)。 如果在呼叫 之後呼叫 DoModal 此函式(只有在 DoModal 傳回 IDOK 時),它會傳回使用者實際選取的內容。

範例

// Get the characteristics of the currently selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   LOGFONT lf;
   dlg.GetCurrentFont(&lf);
   TRACE(_T("Face name of the selected font = %s\n"), lf.lfFaceName);
}

CFontDialog::GetFaceName

呼叫此函式以擷取所選字型的臉部名稱。

CString GetFaceName() const;

傳回值

對話方塊中所選取 CFontDialog 字型的臉部名稱。

範例

// Get the face name of the selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   CString facename = dlg.GetFaceName();
   TRACE(_T("Face name of the selected font = %s\n"), facename);
}

CFontDialog::GetSize

呼叫此函式以擷取選取字型的大小。

int GetSize() const;

傳回值

字型的大小,以點為單位的十分之一。

範例

// Get the size of the selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   int size = dlg.GetSize();
   TRACE(_T("The size of the selected font = %d\n"), size);
}

CFontDialog::GetStyleName

呼叫此函式以擷取所選字型的樣式名稱。

CString GetStyleName() const;

傳回值

字型的樣式名稱。

範例

// Get the style name of the selected font, if any.
CFontDialog dlg;
dlg.m_cf.Flags |= CF_USESTYLE;
if (dlg.DoModal() == IDOK)
{
   CString stylename = dlg.GetStyleName();
   TRACE(_T("Style name of the selected font = %s\n"), stylename);
}

CFontDialog::GetWeight

呼叫此函式以擷取所選字型的粗細。

int GetWeight() const;

傳回值

所選字型的粗細。

備註

如需字型粗細的詳細資訊,請參閱 CFont::CreateFont

範例

// Get the weight of the selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   int weight = dlg.GetWeight();
   TRACE(_T("Weight of the selected font = %d\n"), weight);
}

CFontDialog::IsBold

呼叫此函式以判斷選取的字型是否為粗體。

BOOL IsBold() const;

傳回值

如果選取的字型已啟用 Bold 特性,則為非零;否則為 0。

範例

// Is the selected font bold?
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   BOOL bold = dlg.IsBold();
   TRACE(_T("Is the selected font bold? %d\n"), bold);
}

CFontDialog::IsItalic

呼叫此函式以判斷選取的字型是否為斜體。

BOOL IsItalic() const;

傳回值

如果選取的字型已啟用斜體特性,則為非零;否則為 0。

範例

// Is the selected font italic?
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   BOOL italic = dlg.IsItalic();
   TRACE(_T("Is the selected font italic? %d\n"), italic);
}

CFontDialog::IsStrikeOut

呼叫此函式,以判斷選取的字型是否以刪除線顯示。

BOOL IsStrikeOut() const;

傳回值

如果選取的字型已啟用 Strikeout 特性,則為非零;否則為 0。

範例

// Is the selected font displayed with strikeout?

CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   BOOL strikeout = dlg.IsStrikeOut();
   TRACE(_T("Is the selected font strikeout? %d\n"), strikeout);
}

CFontDialog::IsUnderline

呼叫此函式,以判斷選取的字型是否加上底線。

BOOL IsUnderline() const;

傳回值

如果選取的字型已啟用底線特性,則為非零;否則為 0。

範例

// Is the selected font underlined?
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   BOOL underline = dlg.IsUnderline();
   TRACE(_T("Is the selected font underlined? %d\n"), underline);
}

CFontDialog::m_cf

結構,其成員會儲存對話物件的特性。

CHOOSEFONT m_cf;

備註

CFontDialog 構 物件之後,您可以使用 m_cf 來修改對話方塊的各個層面,再呼叫 DoModal 成員函式。 如需此結構的詳細資訊,請參閱 Windows SDK 中的 CHOOSEFONT

範例

// The code fragment creates a font based on the information
// we got from CFontDialog::m_cf variable.

CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
   // Create the font using the selected font from CFontDialog.
   LOGFONT lf;
   memcpy(&lf, dlg.m_cf.lpLogFont, sizeof(LOGFONT));

   CFont font;
   VERIFY(font.CreateFontIndirect(&lf));

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

另請參閱

MFC 範例 HIERSVR
CCommonDialog 類別
階層架構圖表