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 />

All IDs of the controls are numbers. When writing code to access the controls, we can use these<br />

numbers directly, or we can assign each control a text ID like what we did for sample 7.6\CDB. Actually,<br />

these IDs are all pre-defined, whose symbolic IDs can be found in file “Dlgs.h” (we can find this file under<br />

~DevStudio\VC\Include\ directory). We can check a control’s ID value and search through this file to find<br />

out its symbolic ID. For example, in font dialog box, the combo box under window “Font:” has an ID of<br />

1136 (0x470). In file “Dlgs.h”, we can find:<br />

……<br />

……<br />

//<br />

// Combo boxes.<br />

//<br />

#define cmb1<br />

#define cmb2<br />

#define cmb3<br />

0x0470<br />

0x0471<br />

0x0472<br />

Value 0x0470 is used by cmb1, so we can access this combo box through using symbol cmb1.<br />

Sample 7.8\CDB demonstrates how to use custom dialog template to implement font dialog box. It is<br />

based on sample 7.7\CDB. In the sample, a new command Font Dialog Box | Customize is added to the<br />

application, whose command ID is ID_FONTDIALOGBOX_INITIALIZE. If we execute this command, a font<br />

dialog box whose color selection feature is disabled will be invoked.<br />

In order to implement this dialog box, we need to disable the following two controls: stc4 (Static<br />

control containing string “Color”, whose ID value is 1091) and cmb4 (Combo box for color selection,<br />

whose ID value is 1139).<br />

In the sample, the custom dialog template is IDD_FONT. It contains all the standard controls.<br />

A new class MCFontClass is added to the application through using Class Wizard, its base class is<br />

CFontClass. In the derived class, function OnInitDialog is overridden, within which the above two<br />

controls are disabled:<br />

BOOL MCFontDialog::OnInitDialog()<br />

{<br />

CFontDialog::OnInitDialog();<br />

GetDlgItem(stc4)->ShowWindow(SW_HIDE);<br />

GetDlgItem(cmb4)->ShowWindow(SW_HIDE);<br />

return TRUE;<br />

}<br />

The WM_COMMAND type message handler of command ID_FONTDIALOGBOX_INITIALIZE is CCDBDoc::<br />

OnFontdialogboxCustomize(), which is also added through using Class Wizard. The following is its<br />

implementation:<br />

void CCDBDoc::OnFontdialogboxCustomize()<br />

{<br />

MCFontDialog dlg;<br />

}<br />

dlg.m_cf.lpTemplateName=MAKEINTRESOURCE(IDD_FONT);<br />

dlg.m_cf.Flags|=CF_ENABLETEMPLATE;<br />

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

{<br />

}<br />

With the above implementation, the font dialog box will not contain color selection feature.<br />

192

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

Saved successfully!

Ooh no, something went wrong!