11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7. Common Dialog Boxes<br />

Retrieving Selected Font<br />

After the font dialog box is closed, the font that is selected by the user can be retrieved through<br />

following member functions: CFontDialog::GetFaceName(), CFontDialog::IsBold(), CFongDialog::<br />

IsItalic()….<br />

Sample<br />

Sample 7.7\CDB demonstrates how to use font dialog box. It is based on sample 7.6\CDB with a new<br />

command Font Dialog Box | Initialize added to the application. The ID of this command is<br />

ID_FONTDIALOGBOX_INITIALIZE, and its WM_COMMAND message handler is CCDBDoc::<br />

OnFontdialogboxInitialize(). The command is implement as follows:<br />

void CCDBDoc::OnFontdialogboxInitialize()<br />

{<br />

LOGFONT lf;<br />

CFontDialog dlg;<br />

CString szStr;<br />

COLORREF color;<br />

memset(&lf, 0, sizeof(LOGFONT));<br />

lf.lfItalic=TRUE;<br />

lf.lfUnderline=TRUE;<br />

lf.lfStrikeOut=TRUE;<br />

lf.lfWeight=FW_BOLD;<br />

strcpy(lf.lfFaceName, "Times New Roman");<br />

}<br />

dlg.m_cf.rgbColors=RGB(255, 255, 0);<br />

dlg.m_cf.Flags|=CF_LIMITSIZE|CF_EFFECTS|CF_INITTOLOGFONTSTRUCT;<br />

dlg.m_cf.nSizeMin=20;<br />

dlg.m_cf.nSizeMax=48;<br />

dlg.m_cf.lpLogFont=&lf;<br />

if(dlg.DoModal() == IDOK)<br />

{<br />

color=dlg.GetColor();<br />

szStr.Format("Font Color: R=%d, G=%d, B=%d",<br />

GetRValue(color), GetGValue(color), GetBValue(color));<br />

szStr+="\nFace Name: ";<br />

szStr+=dlg.GetFaceName();<br />

if(dlg.IsItalic() == TRUE)szStr+="\nFont is italic";<br />

if(dlg.IsUnderline() == TRUE)szStr+="\nFont is underlined";<br />

if(dlg.IsStrikeOut() == TRUE)szStr+="\nFont is strike out";<br />

if(dlg.IsBold())szStr+="\nFont is bolded";<br />

AfxMessageBox(szStr);<br />

}<br />

The initially selected font is “Times New Roman”, whose color is yellow (RGB(255, 255, 0)), and<br />

has the following styles: italic, underlined, strikeout, bolded. The range of the font size is restricted<br />

between 20 and 48. After the user clicks button “OK”, the properties of the selected font are retrieved<br />

through member functions of class CFontDialog, and are displayed in a message box.<br />

7.8 Customizing Dialog Box Template<br />

Like file and color dialog boxes, we can use custom dialog template to implement font dialog box.<br />

This gives us a chance to move, resize or hide some standard controls. To use a custom template, we need<br />

the following steps: 1) Design a new dialog template that contains all the controls in a standard font dialog<br />

box. 2) Set CF_ENABLETEMPLATE bit for member Flags of structure CHOOSEFONT, and assign custom template<br />

name to member lpTemplateName. 3) Assign application’s instance handle to member hInstance.<br />

The standard dialog template can be found in file “Commdlg.dll”. It can be opened from Developer<br />

Studio in “Resources” mode.<br />

191

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!