Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
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
- Page 155 and 156: Chapter 5. Common Controls Custom R
- Page 157 and 158: Chapter 6. Dialog Box Chapter 6 Dia
- Page 159 and 160: Chapter 6. Dialog Box void CMLDialo
- Page 161 and 162: Chapter 6. Dialog Box } Function CP
- Page 163 and 164: Chapter 6. Dialog Box Everything is
- Page 165 and 166: Chapter 6. Dialog Box } MINMAXINFO;
- Page 167 and 168: Chapter 6. Dialog Box Sample Sampel
- Page 169 and 170: Chapter 6. Dialog Box } brush=(HBRU
- Page 171 and 172: Chapter 6. Dialog Box wndA.GetWindo
- Page 173 and 174: Chapter 6. Dialog Box rectStaticGrp
- Page 175 and 176: Chapter 6. Dialog Box } TOOLTIPTEXT
- Page 177 and 178: Chapter 6. Dialog Box ) { } if ( )
- Page 179 and 180: Chapter 6. Dialog Box pWndCtrl=GetW
- Page 181 and 182: Chapter 6. Dialog Box Sample 6.8-2\
- Page 183 and 184: Chapter 6. Dialog Box } // handle t
- Page 185 and 186: Chapter 6. Dialog Box 1) Tracking s
- Page 187 and 188: Chapter 7. Common Dialog Boxes DWOR
- Page 189 and 190: Chapter 7. Common Dialog Boxes } {
- Page 191 and 192: Chapter 7. Common Dialog Boxes } }
- Page 193 and 194: Chapter 7. Common Dialog Boxes dial
- Page 195 and 196: Chapter 7. Common Dialog Boxes Alth
- Page 197 and 198: Chapter 7. Common Dialog Boxes } NU
- Page 199 and 200: Chapter 7. Common Dialog Boxes Stc3
- Page 201 and 202: Chapter 7. Common Dialog Boxes DWOR
- Page 203 and 204: Chapter 7. Common Dialog Boxes the
- Page 205: Chapter 7. Common Dialog Boxes Comm
- Page 209 and 210: Chapter 7. Common Dialog Boxes 7.9
- Page 211 and 212: Chapter 7. Common Dialog Boxes } }
- Page 213 and 214: Chapter 7. Common Dialog Boxes …
- Page 215 and 216: Chapter 7. Common Dialog Boxes …
- Page 217 and 218: Chapter 8. DC, Pen, Brush and Palet
- Page 219 and 220: Chapter 8. DC, Pen, Brush and Palet
- Page 221 and 222: Chapter 8. DC, Pen, Brush and Palet
- Page 223 and 224: Chapter 8. DC, Pen, Brush and Palet
- Page 225 and 226: Chapter 8. DC, Pen, Brush and Palet
- Page 227 and 228: Chapter 8. DC, Pen, Brush and Palet
- Page 229 and 230: Chapter 8. DC, Pen, Brush and Palet
- Page 231 and 232: Chapter 8. DC, Pen, Brush and Palet
- Page 233 and 234: Chapter 8. DC, Pen, Brush and Palet
- Page 235 and 236: Chapter 8. DC, Pen, Brush and Palet
- Page 237 and 238: Chapter 8. DC, Pen, Brush and Palet
- Page 239 and 240: Chapter 8. DC, Pen, Brush and Palet
- Page 241 and 242: Chapter 8. DC, Pen, Brush and Palet
- Page 243 and 244: Chapter 8. DC, Pen, Brush and Palet
- Page 245 and 246: Chapter 8. DC, Pen, Brush and Palet
- Page 247 and 248: Chapter 8. DC, Pen, Brush and Palet
- Page 249 and 250: Chapter 8. DC, Pen, Brush and Palet
- Page 251 and 252: Chapter 8. DC, Pen, Brush and Palet
- Page 253 and 254: Chapter 9. Font Here, member lfFace
- Page 255 and 256: Chapter 9. Font } dlg.m_bBgdStyle=m
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