anCFontDialog类封装了Windows字体对话框。CFontDialog对象是一个对话框,其中包含系统中当前安装的字体列表,用户可以从列表中选择要用的字体,同时在字体对话框中还可以设置字体大小、颜色、效果、和字符集等属性。构造CFontDialog对象,通过构造函数实现,CFontDialog构造函的原型如下:

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

lplfInitial LOGFONT 结构体指针,用于设置默认字体。

dwFlags 用于控制对话框行为。

pdcPrinter 指向打印机设备上下文的指针。如果提供,此参数将指向要选择字体的打印机的设备上下文。

pParentWnd 指向对话框父窗口的指针。

一旦一个CFontDialog对象被构造,就可以通过m_cf 结构对象设置对话框的初始化值。m_cf是CHOOSEFONT型。CHOOSEFONT结构包含用于初始化字体对话框的信息。用户关闭对话框后,系统将有关用户选择的信息返回在此结构中。CHOOSEFONT结构如下:

typedef struct { DWORD        lStructSize; HWND         hwndOwner; HDC          hDC; LPLOGFONT    lpLogFont; INT          iPointSize; DWORD        Flags; COLORREF     rgbColors; LPARAM       lCustData; LPCFHOOKPROC lpfnHook; LPCTSTR      lpTemplateName; HINSTANCE    hInstance; LPTSTR       lpszStyle; WORD         nFontType; WORD         ___MISSING_ALIGNMENT__; INT          nSizeMin; INT          nSizeMax;
} CHOOSEFONT, *LPCHOOSEFONT;

成员说明(摘自MSDN Library):

lStructSize

Specifies the length, in bytes, of the structure.

hwndOwner

Handle to the window that owns the dialog box. This member can be any valid window handle, or it can be NULL if the dialog box has no owner.

hDC

Handle to the device context (or information context) of the printer whose fonts will be listed in the dialog box. This member is used only if the Flags member specifies the CF_PRINTERFONTS or CF_BOTH flag; otherwise, this member is ignored.

lpLogFont

Pointer to a LOGFONT structure. If you set the CF_INITTOLOGFONTSTRUCT flag in the Flags member and initialize the LOGFONT members, the ChooseFont function initializes the dialog box with a font that is the closest possible match. If the user clicks the OK button, ChooseFont sets the members of the LOGFONT structure based on the user's selections.

iPointSize

Specifies the size of the selected font, in units of 1/10 of a point. The ChooseFont function sets this value after the user closes the dialog box.

Flags

A set of bit flags that you can use to initialize the Font dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be one or more of the following values.

Value Meaning
CF_APPLY Causes the dialog box to display the Apply button. You should provide a hook procedure to process WM_COMMAND messages for the Apply button. The hook procedure can send the WM_CHOOSEFONT_GETLOGFONT message to the dialog box to retrieve the address of the LOGFONT structure that contains the current selections for the font.
CF_ANSIONLY This flag is obsolete. To limit font selections to all scripts except those that use the OEM or Symbol character sets, use CF_SCRIPTSONLY. To get the original CF_ANSIONLY behavior, use CF_SELECTSCRIPT and specify ANSI_CHARSET in the lfCharSet member of the LOGFONT structure pointed to by lpLogFont.
CF_BOTH Causes the dialog box to list the available printer and screen fonts. The hDC member is a handle to the device context (or information context) associated with the printer. This flag is a combination of the CF_SCREENFONTS and CF_PRINTERFONTS flags.
CF_TTONLY Specifies that ChooseFont should only enumerate and allow the selection of TrueType fonts.
CF_EFFECTS Causes the dialog box to display the controls that allow the user to specify strikeout, underline, and text color options. If this flag is set, you can use the rgbColors member to specify the initial text color. You can use the lfStrikeOut and lfUnderline members of the LOGFONT structure pointed to by lpLogFont to specify the initial settings of the strikeout and underline check boxes. ChooseFont can use these members to return the user's selections.
CF_ENABLEHOOK Enables the hook procedure specified in the lpfnHook member of this structure.
CF_ENABLETEMPLATE Indicates that the hInstance and lpTemplateName members specify a dialog box template to use in place of the default template.
CF_ENABLETEMPLATEHANDLE Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName member if this flag is specified.
CF_FIXEDPITCHONLY Specifies that ChooseFont should select only fixed-pitch fonts.
CF_FORCEFONTEXIST Specifies that ChooseFont should indicate an error condition if the user attempts to select a font or style that does not exist.
CF_INITTOLOGFONTSTRUCT Specifies that ChooseFont should use the LOGFONT structure pointed to by the lpLogFont member to initialize the dialog box controls.
CF_LIMITSIZE Specifies that ChooseFont should select only font sizes within the range specified by the nSizeMin and nSizeMax members.
CF_NOOEMFONTS Same as the CF_NOVECTORFONTS flag.
CF_NOFACESEL When using a LOGFONT structure to initialize the dialog box controls, use this flag to selectively prevent the dialog box from displaying an initial selection for the font name combo box. This is useful when there is no single font name that applies to the text selection.
CF_NOSCRIPTSEL Disables the Script combo box. When this flag is set, the lfCharSet member of the LOGFONT structure is set to DEFAULT_CHARSET when ChooseFont returns. This flag is used only to initialize the dialog box.
CF_NOSTYLESEL When using a LOGFONT structure to initialize the dialog box controls, use this flag to selectively prevent the dialog box from displaying an initial selection for the font style combo box. This is useful when there is no single font style that applies to the text selection.
CF_NOSIZESEL When using a LOGFONT structure to initialize the dialog box controls, use this flag to selectively prevent the dialog box from displaying an initial selection for the font size combo box. This is useful when there is no single font size that applies to the text selection.
CF_NOSIMULATIONS Specifies that ChooseFont should not allow graphics device interface (GDI) font simulations.
CF_NOVECTORFONTS Specifies that ChooseFont should not allow vector font selections.
CF_NOVERTFONTS Causes the Font dialog box to list only horizontally oriented fonts.
CF_PRINTERFONTS Causes the dialog box to list only the fonts supported by the printer associated with the device context (or information context) identified by the hDC member.
CF_SCALABLEONLY Specifies that ChooseFont should allow only the selection of scalable fonts. (Scalable fonts include vector fonts, scalable printer fonts, TrueType fonts, and fonts scaled by other technologies.)
CF_SCREENFONTS Causes the dialog box to list only the screen fonts supported by the system.
CF_SCRIPTSONLY Specifies that ChooseFont should allow selection of fonts for all non-OEM and Symbol character sets, as well as the ANSI character set. This supersedes the CF_ANSIONLY value.
CF_SELECTSCRIPT When specified on input, only fonts with the character set identified in the lfCharSet member of the LOGFONT structure are displayed. The user will not be allowed to change the character set specified in the Scripts combo box.
CF_SHOWHELP Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button.
CF_USESTYLE Specifies that the lpszStyle member is a pointer to a buffer that contains style data that ChooseFont should use to initialize the Font Style combo box. When the user closes the dialog box, ChooseFont copies style data for the user's selection to this buffer.
CF_WYSIWYG Specifies that ChooseFont should allow only the selection of fonts available on both the printer and the display. If this flag is specified, the CF_BOTH and CF_SCALABLEONLY flags should also be specified.

rgbColors

If the CF_EFFECTS flag is set, rgbColors specifies the initial text color. When ChooseFont returns successfully, this member contains the RGB value of the text color the user selected. To create a COLORREF color value, use the RGB macro.

lCustData

Specifies application-defined data that the system passes to the hook procedure identified by the lpfnHook member. When the system sends the WM_INITDIALOG message to the hook procedure, the message's lParam parameter is a pointer to the CHOOSEFONT structure specified when the dialog was created. The hook procedure can use this pointer to get the lCustData value.

lpfnHook

Pointer to a CFHookProc hook procedure that can process messages intended for the dialog box. This member is ignored unless the CF_ENABLEHOOK flag is set in the Flags member.

lpTemplateName

Pointer to a null-terminated string that names the dialog box template resource in the module identified by the hInstance member. This template is substituted for the standard dialog box template. For numbered dialog box resources, lpTemplateName can be a value returned by the MAKEINTRESOURCE macro. This member is ignored unless the CF_ENABLETEMPLATE flag is set in the Flags member.

hInstance

If the CF_ENABLETEMPLATEHANDLE flag is set in the Flags member, hInstance is a handle to a memory object containing a dialog box template. If the CF_ENABLETEMPLATE flag is set, hInstance is a handle to a module that contains a dialog box template named by the lpTemplateName member. If neither CF_ENABLETEMPLATEHANDLE nor CF_ENABLETEMPLATE is set, this member is ignored.

lpszStyle

Pointer to a buffer that contains style data. If the CF_USESTYLE flag is specified, ChooseFont uses the data in this buffer to initialize the font style combo box. When the user closes the dialog box, ChooseFont copies the string in the font style combo box into this buffer.

nFontType

Specifies the type of the selected font when ChooseFont returns. This member can be one or more of the following values.

Value Meaning
BOLD_FONTTYPE The font weight is bold. This information is duplicated in the lfWeight member of the LOGFONT structure and is equivalent to FW_BOLD.
ITALIC_FONTTYPE The italic font attribute is set. This information is duplicated in the lfItalic member of the LOGFONT structure.
PRINTER_FONTTYPE The font is a printer font.
REGULAR_FONTTYPE The font weight is normal. This information is duplicated in the lfWeight member of the LOGFONT structure and is equivalent to FW_REGULAR.
SCREEN_FONTTYPE The font is a screen font.
SIMULATED_FONTTYPE The font is simulated by the graphics device interface (GDI).

nSizeMin

Specifies the minimum point size a user can select. ChooseFont recognizes this member only if the CF_LIMITSIZE flag is specified.

nSizeMax

Specifies the maximum point size a user can select. ChooseFont recognizes this member only if the CF_LIMITSIZE flag is specified.

CFontDialog类的成员函数

Construction

CFontDialog Constructs a CFontDialog object.

Operations

DoModal Displays the dialog and allows the user to make a selection.
GetCurrentFont Retrieves the name of the currently selected font.
GetFaceName Returns the face name of the selected font.
GetStyleName Returns the style name of the selected font.
GetSize Returns the point size of the selected font.
GetColor Returns the color of the selected font.
GetWeight Returns the weight of the selected font.
IsStrikeOut Determines whether the font is displayed with strikeout.
IsUnderline Determines whether the font is underlined.
IsBold Determines whether the font is bold.
IsItalic Determines whether the font is italic.

显示字体对话框通过DoModal函数实现。

字体对话框示例

1.在单文档工程的IDR_MAINFRAME菜单中,新建FontDialogTest菜单及子菜单,如下:

2. 在View类中新加5个变量,如下:

并在构造函数中将drawType初始化为0,如下:

CFileDialogTestView::CFileDialogTestView() noexcept
{// TODO: 在此处添加构造代码drawType = 0;
}

3. 为子菜单”Font“添加事件处理函数,如下:

代码如下:

void CFileDialogTestView::OnFont()
{// TODO: 在此添加命令处理程序代码CFontDialog fdlg(&mLogfont);fdlg.m_cf.rgbColors = mCurrentCr;if (fdlg.DoModal() == IDOK){fdlg.GetCurrentFont(&mLogfont);mCurrentCr = fdlg.GetColor();}
}

4. 为子菜单”Single Line Textt“添加事件处理函数。

代码如下:

void CFileDialogTestView::OnSingleLineText()
{// TODO: 在此添加命令处理程序代码drawType = 1;
}

5. 添加消息处理函数OnLButtonDown(UINT nFlags, CPoint point),代码如下:

void CFileDialogTestView::OnLButtonDown(UINT nFlags, CPoint point)
{// TODO: 在此添加消息处理程序代码和/或调用默认值CClientDC dc(this);switch(drawType){case 1:TEXTMETRIC tm;dc.GetTextMetrics(&tm);CreateSolidCaret(tm.tmAveCharWidth / 8, tm.tmHeight);//CreateSolidCaret(mLogfont.lfWidth/8, mLogfont.lfHeight);SetCaretPos(point);ShowCaret();startPoint = point;mstr.Empty();break;default:break;}CView::OnLButtonDown(nFlags, point);
}

7.添加消息处理函数OnChar(UINT nChar, UINT nRepCnt, UINT nFlags),代码如下:

void CFileDialogTestView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{// TODO: 在此添加消息处理程序代码和/或调用默认值if(drawType==1){CClientDC dc(this);COLORREF oldCr;oldCr = dc.SetTextColor(mCurrentCr);CFont mfont;CFont* pOldfont;mfont.CreateFontIndirectW(&mLogfont);pOldfont = dc.SelectObject(&mfont);if (VK_BACK == nChar){COLORREF clr = dc.SetTextColor(dc.GetBkColor());dc.TextOutW(startPoint.x, startPoint.y, mstr);mstr = mstr.Left(mstr.GetLength() - 1);dc.SetTextColor(clr);CSize sz = dc.GetTextExtent(mstr);CPoint temPoint;temPoint.x = startPoint.x + sz.cx;temPoint.y = startPoint.y;HideCaret();dc.TextOutW(startPoint.x, startPoint.y, mstr);SetCaretPos(temPoint);ShowCaret();}else if (VK_ESCAPE == nChar){drawType = 0;HideCaret();}else if (VK_RETURN == nChar){drawType = 0;HideCaret();dc.TextOutW(startPoint.x, startPoint.y, mstr);}else{mstr += (TCHAR)nChar;CSize sz = dc.GetTextExtent(mstr);CPoint temPoint;temPoint.x = startPoint.x + sz.cx;temPoint.y = startPoint.y;HideCaret();dc.TextOutW(startPoint.x, startPoint.y, mstr);SetCaretPos(temPoint);ShowCaret();}dc.SetTextColor(oldCr);}CView::OnChar(nChar, nRepCnt, nFlags);
}

按Ctrl+F5执行程序,如下:

 点击菜单“FontDialogTest”下的子菜单“Font”,选择设置字体

打开字体对话框,设置字体 如下:

点击菜单“FontDialogTest”下的子菜单“Single Line Text”, 然后用鼠标左键,点击屏幕,确定输入文字位置,在点击鼠标位置会出现插入符,输入文字后,按回车键,即得到,设置字体的输入文字,如下:

MFC 通用对话框之字体对话框相关推荐

  1. java中如何调出字体对话框_java 字体对话框

    本人不才!花了很长时间写了这个代码.不是很好,比起Windows自带的字体对话框差一点. 不过已经够用了.等将来有时间了,在进行完善. 如果有什么好的建议,可以提.本人再补. 预览效果如下图: pac ...

  2. 获取对话框当前cfont_MFC设置对话框、字体对话框、颜色对话框(转)

    一.绘图 新建一个工程Graphic.添加一个菜单"绘图",在这个菜单中添加四个菜单项,分别为点(IDM_DOT).直线(IDM_LINE).矩形(IDM_RECTANGLE)和椭 ...

  3. 获取对话框当前cfont_获取对话框当前cfont_MFC设置对话框、字体对话框、颜色对话框(转)...

    一.绘图 新建一个工程Graphic.添加一个菜单"绘图",在这个菜单中添加四个菜单项,分别为点(IDM_DOT).直线(IDM_LINE).矩形(IDM_RECTANGLE)和椭 ...

  4. 基础语法篇_10——设置对话框、颜色对话框、字体对话框、示例对话框、改变对话框和控件的背景及文本颜色、位图显示

  5. VB字体对话框ShowFont

    " 本节课程学习ShowFont字体对话框" 上节课程一起学习了ShowColor颜色对话框,本节课程将学习的是字体对话框. 字体对话框,为用户提供一个字体选择器,用于字体.大小. ...

  6. MFC—对话框程序—模式对话框与非模式对话框

    一.根据主窗口类型,MFC软件工程可以分为以下几种架构模型: 1.SDI(Single Document Interface):单文档界面,一个主框架窗口下只能编辑一份文档. 例如:记事本和画笔等. ...

  7. VC6重载字体对话框,去除颜色下拉框

    为了去除字体对话框中的颜色下拉框,我们需要重载字体对话框类:CFontDialog 1. 创建一个MFC Class,继承自CFontDialog: // MyFontDialong.h : head ...

  8. VS2010/MFC编程入门之十七(对话框:文件对话框)

    上一讲鸡啄米介绍的是消息对话框,本节讲解文件对话框.文件对话框也是很常用的一类对话框. 文件对话框的分类       文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见 ...

  9. PyQt5 技术篇-调用字体对话框(QFontDialog)获取字体,控件设置字体。

    QFontDialog字体对话框 font, isok = QFontDialog.getFont() 返回值font是QFont字体对象,isok是True或False. 注:如果点了确定就是Tru ...

  10. MFC 基础知识:主对话框与子对话框(二)

    (仅供使用VS MFC初学者学习,高手莫见笑) 接"MFC 基础知识:主对话框与子对话框(一)"的内容 在"MFC 基础知识:主对话框与子对话框(一)"中Edi ...

最新文章

  1. Redis 击穿、穿透、雪崩产生原因以及解决思路
  2. 补天白帽大会:无处不在的无线电攻击
  3. 个人阅读作业Week7
  4. malloc 两种实现方式:brk 和 mmap
  5. ImageUploadAssist--DATA
  6. keepalived VS zookeeper
  7. es6 Object.is()方法
  8. You have new mail in /var/spool/mail/root 解决方法
  9. 最全面的Linux命令大全出炉了
  10. Oracle10g卸载
  11. android系统解压zip文件,如何在Android手机上解压缩rar / zip文件
  12. 装IDT声卡时总是说检测到的硬件不受此IDT软件程序包支持安装将终止
  13. Typora最后的免费版本
  14. 如何让spyder中途暂停(之前运行出来的结果仍然显示)
  15. 小波变换:基于matlab的数字图像水印嵌入及提取(灰度图)
  16. Pascal与C的指针
  17. 计算机病毒发展趋势论文,计算机病毒的发展趋势及对策
  18. xjoi1.4行李托运
  19. 计算机环境下会计循环的八个流程,会计循环的基本步骤和流程是怎样的
  20. matlab抛物偏微分方程,抛物型方程差分求解 跪求MATLAB解抛物型偏微分方程的程序...

热门文章

  1. html 科赫雪花,CSS3 科赫雪花分形V1
  2. ETL工具kettle实现数据同步
  3. 《 Deep Learning 》全文翻译 Yann LeCun, Yoshua Bengio, Geoffrey Hinton
  4. 如何批量创建文件夹并命名?
  5. IQtree:使用 SNP 数据构建 有根 系统发育树及踩坑
  6. 前端和后端的英文_前端工程师和后端工程师
  7. Unix基本操作..Hash-网络攻防大赛笔记(2)
  8. z8350cpu linux,跑分 Intel Z8350小有提升_平板电脑评测-中关村在线
  9. 群控源码源代码,微群控系统源码的实现原理,核心源码实现 核心框架,可二次开发
  10. 【obs owt】屏幕采集创建DXGI