MFC 通用对话框之字体对话框

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.

ValueMeaning
CF_APPLYCauses 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_ANSIONLYThis 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_BOTHCauses 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_TTONLYSpecifies that ChooseFont should only enumerate and allow the selection of TrueType fonts.
CF_EFFECTSCauses 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_ENABLEHOOKEnables the hook procedure specified in the lpfnHook member of this structure.
CF_ENABLETEMPLATEIndicates that the hInstance and lpTemplateName members specify a dialog box template to use in place of the default template.
CF_ENABLETEMPLATEHANDLEIndicates 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_FIXEDPITCHONLYSpecifies that ChooseFont should select only fixed-pitch fonts.
CF_FORCEFONTEXISTSpecifies that ChooseFont should indicate an error condition if the user attempts to select a font or style that does not exist.
CF_INITTOLOGFONTSTRUCTSpecifies that ChooseFont should use the LOGFONT structure pointed to by the lpLogFont member to initialize the dialog box controls.
CF_LIMITSIZESpecifies that ChooseFont should select only font sizes within the range specified by the nSizeMin and nSizeMax members.
CF_NOOEMFONTSSame as the CF_NOVECTORFONTS flag.
CF_NOFACESELWhen 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_NOSCRIPTSELDisables 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_NOSTYLESELWhen 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_NOSIZESELWhen 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_NOSIMULATIONSSpecifies that ChooseFont should not allow graphics device interface (GDI) font simulations.
CF_NOVECTORFONTSSpecifies that ChooseFont should not allow vector font selections.
CF_NOVERTFONTSCauses the Font dialog box to list only horizontally oriented fonts.
CF_PRINTERFONTSCauses 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_SCALABLEONLYSpecifies 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_SCREENFONTSCauses the dialog box to list only the screen fonts supported by the system.
CF_SCRIPTSONLYSpecifies 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_SELECTSCRIPTWhen 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_SHOWHELPCauses 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_USESTYLESpecifies 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_WYSIWYGSpecifies 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.

ValueMeaning
BOLD_FONTTYPEThe font weight is bold. This information is duplicated in the lfWeight member of the LOGFONT structure and is equivalent to FW_BOLD.
ITALIC_FONTTYPEThe italic font attribute is set. This information is duplicated in the lfItalic member of the LOGFONT structure.
PRINTER_FONTTYPEThe font is a printer font.
REGULAR_FONTTYPEThe font weight is normal. This information is duplicated in the lfWeight member of the LOGFONT structure and is equivalent to FW_REGULAR.
SCREEN_FONTTYPEThe font is a screen font.
SIMULATED_FONTTYPEThe 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

CFontDialogConstructs a CFontDialog object.

Operations

DoModalDisplays the dialog and allows the user to make a selection.
GetCurrentFontRetrieves the name of the currently selected font.
GetFaceNameReturns the face name of the selected font.
GetStyleNameReturns the style name of the selected font.
GetSizeReturns the point size of the selected font.
GetColorReturns the color of the selected font.
GetWeightReturns the weight of the selected font.
IsStrikeOutDetermines whether the font is displayed with strikeout.
IsUnderlineDetermines whether the font is underlined.
IsBoldDetermines whether the font is bold.
IsItalicDetermines 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”, 然后用鼠标左键,点击屏幕,确定输入文字位置,在点击鼠标位置会出现插入符,输入文字后,按回车键,即得到,设置字体的输入文字,如下:

 

 

 

  • 0
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论
' 功 能:不使用控件,对Windows通用对话框进行自定义,核心包括一个封装的类和两个模块。 [更新历史] ◆ Ver 1.0.2 开发时间:2008-09-24 09:27 ~ 2008-09-24 12:04 ' 1、修正了没有设置预览或程序标志图片框时,对话框位置无法调整的问题; ' 2、增加了图片预览按比例显示,并显示图片宽x高和预览比例。 ' 3、增加参考资料:VB 取得图片大小 ◆ Ver 1.0.1 开发时间:2008-09-21 17:17 ~ 2008-09-22 17:41 ' 1、增加了字体对话框(预览时,几个下拉框要双击才能立即见到改变,字体颜色无法预览); ' 2、增加了颜色对话框 ' 特别注意:上面的两个对话框没有经过仔细的测试,可能使用时会遇到未知的问题! ' 注意:源代码(贺兰_通用对话框 Ver 1.0.1.rar)中不含参考资料和3张图片,若需要,请参考 Ver 1.0.0 ◆ Ver 1.0.0 开发时间:2008-09-16 15:17 ~ 2008-09-21 16:09 ' CCommonDialog.cls ' 功 能:使用 Windows 通用对话框,如下: ' 0、文件属性对话框 ' 1、打开对话框(可以提供某些文件预览) ' 2、保存对话框 ' 3、字体对话框(预览?) ' 4、颜色对话框 ' Ver 1.0.0 版本,只实现了0、1、2功能,3、4功能以后再做。因为一般打开、保存对话框用得多。 ' MDrawWaves.bas ' 功 能:给定一个Wave文件,画出其波形。 ' MCDHook.bas ' 功 能:对话框预览核心模块,实现回调函数,消息截取处理和其他功能。 ' 注 意:它里面的变量、函数等,一般不需要在外部调用,所有功能基本封装在 CCommonDialog 类中。 ' frmMain.frm ' 程序主窗体,演示 CCommonDialog 类的各种属性和方法。 还包括以下参考资料: CommonDialog Enhanced Callback PaintPicture VB API创建窗口控件 播放.WAV文件,并显示其波形 打开对话框中选多文件 使用API创建Windows窗口控件 用API实现WINDOWS下的通用对话框 用MCI命令来实现多媒体的播放功能 增强型打开_保存对话框类 自定义系统的打开对话框 wave格式详解.txt 常见的影音及图片文件后缀名(按字母顺序排列).txt 利用MFC的CFileDialog生成Windows2000文件对话框.txt 音频文件常见后缀.txt 自定义VB系统控件.txt

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字50 设计师:CSDN官方博客 返回首页
评论

打赏作者

Bill66

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值