Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 7. Common Dialog Boxes DWORD lStructSize; HWND hwndOwner; HDC hDC; LPLOGFONT lpLogFont; INT iPointSize; DWORD Flags; DWORD rgbColors; LPARAM lCustData; LPCFHOOKPROC lpfnHook; LPCTSTR lpTemplateName; HINSTANCE hInstance; LPTSTR lpszStyle; WORD nFontType; WORD ___MISSING_ALIGNMENT__; INT nSizeMin; INT nSizeMax; } CHOOSEFONT; There are several things that can be customized here. First, we can change the default font size range. By default, a valid size for all fonts is ranged from 8 to 72. We can set a narrower range by setting CF_LIMITSIZE bit of member Flags and assigning lower and higher boundaries to members nSizeMin and nSizeMax respectively. We can also specify font’s initial color by setting CF_EFFECTS bit of member Flags and assigning a COLORREF value to member rgbColors (the initial color must be one of the standard colors defined in the font dialog box such as red, green, cyan, black...). Structure LOGFONT Also, we can specify an initially selected font (with specified size and styles) by assigning a LOGFONT type pointer to member lpLogFont. Here, structure LOGFONT is used to describe a font: typedef struct tagLOGFONT { LONG lfHeight; LONG lfWidth; LONG lfEscapement; LONG lfOrientation; LONG lfWeight; BYTE lfItalic; BYTE lfUnderline; BYTE lfStrikeOut; BYTE lfCharSet; BYTE lfOutPrecision; BYTE lfClipPrecision; BYTE lfQuality; BYTE lfPitchAndFamily; CHAR lfFaceName[LF_FACESIZE]; } LOGFONT; A font has many properties. The most important ones are face name, height, and font styles (italic, bolded, underlined or strikeout). In structure LOGFONT, these styles are represented by the following members: lfFaceName, lfWeight, lfItalic, lfUnderline and lfStrikeOut. A face name is the name of the font, it distinguishes one font from another. Every font has its own face name, such as “Arial”, “System” and “MS Serif”. The weight of a font specifies how font is emphasized, its range is from 0 to 1000. Usually we use predefined values such as FW_BOLD (font is bolded) and FW_NORMAL (font is not bolded) for convenience. Member lfItalic, lfUnderline and lfStrikeOut are all Boolean type, by setting them to TRUE, we can let the font to become italic, underlined, or strikeout. Member nSizeMin and nSizeMax can be used to restrict the size of a font. In order to use LOGFONT object to initialize a font dialog box, we must first set CF_INITTOLOGFONTSTRUCT bit for member Flags of structure CHOOSEFONT, then assign the LOGFONT type pointer to member lpLogFont. 190

Chapter 7. Common Dialog Boxes Retrieving Selected Font After the font dialog box is closed, the font that is selected by the user can be retrieved through following member functions: CFontDialog::GetFaceName(), CFontDialog::IsBold(), CFongDialog:: IsItalic()…. Sample Sample 7.7\CDB demonstrates how to use font dialog box. It is based on sample 7.6\CDB with a new command Font Dialog Box | Initialize added to the application. The ID of this command is ID_FONTDIALOGBOX_INITIALIZE, and its WM_COMMAND message handler is CCDBDoc:: OnFontdialogboxInitialize(). The command is implement as follows: void CCDBDoc::OnFontdialogboxInitialize() { LOGFONT lf; CFontDialog dlg; CString szStr; COLORREF color; memset(&lf, 0, sizeof(LOGFONT)); lf.lfItalic=TRUE; lf.lfUnderline=TRUE; lf.lfStrikeOut=TRUE; lf.lfWeight=FW_BOLD; strcpy(lf.lfFaceName, "Times New Roman"); } dlg.m_cf.rgbColors=RGB(255, 255, 0); dlg.m_cf.Flags|=CF_LIMITSIZE|CF_EFFECTS|CF_INITTOLOGFONTSTRUCT; dlg.m_cf.nSizeMin=20; dlg.m_cf.nSizeMax=48; dlg.m_cf.lpLogFont=&lf; if(dlg.DoModal() == IDOK) { color=dlg.GetColor(); szStr.Format("Font Color: R=%d, G=%d, B=%d", GetRValue(color), GetGValue(color), GetBValue(color)); szStr+="\nFace Name: "; szStr+=dlg.GetFaceName(); if(dlg.IsItalic() == TRUE)szStr+="\nFont is italic"; if(dlg.IsUnderline() == TRUE)szStr+="\nFont is underlined"; if(dlg.IsStrikeOut() == TRUE)szStr+="\nFont is strike out"; if(dlg.IsBold())szStr+="\nFont is bolded"; AfxMessageBox(szStr); } The initially selected font is “Times New Roman”, whose color is yellow (RGB(255, 255, 0)), and has the following styles: italic, underlined, strikeout, bolded. The range of the font size is restricted between 20 and 48. After the user clicks button “OK”, the properties of the selected font are retrieved through member functions of class CFontDialog, and are displayed in a message box. 7.8 Customizing Dialog Box Template Like file and color dialog boxes, we can use custom dialog template to implement font dialog box. This gives us a chance to move, resize or hide some standard controls. To use a custom template, we need the following steps: 1) Design a new dialog template that contains all the controls in a standard font dialog box. 2) Set CF_ENABLETEMPLATE bit for member Flags of structure CHOOSEFONT, and assign custom template name to member lpTemplateName. 3) Assign application’s instance handle to member hInstance. The standard dialog template can be found in file “Commdlg.dll”. It can be opened from Developer Studio in “Resources” mode. 191

Chapter 7. Common Dialog Boxes<br />

DWORD lStructSize;<br />

HWND hwndOwner;<br />

HDC hDC;<br />

LPLOGFONT lpLogFont;<br />

INT iPointSize;<br />

DWORD Flags;<br />

DWORD rgbColors;<br />

LPARAM lCustData;<br />

LPCFHOOKPROC lpfnHook;<br />

LPCTSTR lpTemplateName;<br />

HINSTANCE hInstance;<br />

LPTSTR lpszStyle;<br />

WORD nFontType;<br />

WORD ___MISSING_ALIGNMENT__;<br />

INT nSizeMin;<br />

INT nSizeMax;<br />

} CHOOSEFONT;<br />

There are several things that can be customized here. First, we can change the default font size range.<br />

By default, a valid size for all fonts is ranged from 8 to 72. We can set a narrower range by setting<br />

CF_LIMITSIZE bit of member Flags and assigning lower and higher boundaries to members nSizeMin and<br />

nSizeMax respectively. We can also specify font’s initial color by setting CF_EFFECTS bit of member Flags<br />

and assigning a COLORREF value to member rgbColors (the initial color must be one of the standard colors<br />

defined in the font dialog box such as red, green, cyan, black...).<br />

Structure LOGFONT<br />

Also, we can specify an initially selected font (with specified size and styles) by assigning a LOGFONT<br />

type pointer to member lpLogFont. Here, structure LOGFONT is used to describe a font:<br />

typedef struct tagLOGFONT {<br />

LONG lfHeight;<br />

LONG lfWidth;<br />

LONG lfEscapement;<br />

LONG lfOrientation;<br />

LONG lfWeight;<br />

BYTE lfItalic;<br />

BYTE lfUnderline;<br />

BYTE lfStrikeOut;<br />

BYTE lfCharSet;<br />

BYTE lfOutPrecision;<br />

BYTE lfClipPrecision;<br />

BYTE lfQuality;<br />

BYTE lfPitchAndFamily;<br />

CHAR lfFaceName[LF_FACESIZE];<br />

} LOGFONT;<br />

A font has many properties. The most important ones are face name, height, and font styles (italic,<br />

bolded, underlined or strikeout). In structure LOGFONT, these styles are represented by the following<br />

members: lfFaceName, lfWeight, lfItalic, lfUnderline and lfStrikeOut. A face name is the name of<br />

the font, it distinguishes one font from another. Every font has its own face name, such as “Arial”,<br />

“System” and “MS Serif”. The weight of a font specifies how font is emphasized, its range is from 0 to<br />

1000. Usually we use predefined values such as FW_BOLD (font is bolded) and FW_NORMAL (font is not<br />

bolded) for convenience. Member lfItalic, lfUnderline and lfStrikeOut are all Boolean type, by<br />

setting them to TRUE, we can let the font to become italic, underlined, or strikeout. Member nSizeMin and<br />

nSizeMax can be used to restrict the size of a font.<br />

In order to use LOGFONT object to initialize a font dialog box, we must first set<br />

CF_INITTOLOGFONTSTRUCT bit for member Flags of structure CHOOSEFONT, then assign the LOGFONT type<br />

pointer to member lpLogFont.<br />

190

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

Saved successfully!

Ooh no, something went wrong!