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”, 然后用鼠标左键,点击屏幕,确定输入文字位置,在点击鼠标位置会出现插入符,输入文字后,按回车键,即得到,设置字体的输入文字,如下:

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bill66

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值