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

Old Style<br />

If we are writing code for Win32 applications, the above-mentioned method does not work. We must<br />

use the old style file dialog box to implement folder selection.<br />

The default dialog box has two list boxes, one is used for displaying directory names, the other for<br />

displaying file names. One way to implement directory selection dialog box is to replace the standard<br />

dialog template with our own. To avoid any inconsistency, we must include all the controls contained in the<br />

standard template in the custom dialog template (with the same resource IDs), hide the controls that we<br />

don’t want, and override the default class to change its behavior.<br />

To use a user-designed dialog template, we must: 1) Prepare a custom dialog template that has all the<br />

standard controls. 2) Set OFN_ENABLETEMPLATE bit for member Flags of structure OPENFILENAME, assign<br />

custom dialog template name to member lpTemplateName (If the dialog has an integer ID, we need to use<br />

MAKEINTRESOURCE macro to convert it to a string ID). 3) Assign the instance handle of the application to<br />

member hInstance, which can be obtained by calling function AfxGetInstanceHandle().<br />

The standard file dialog box has two list boxes that are used to display files and directories, two combo<br />

boxes to display drives and file types, an edit box to display file name, a “Read only” check box, several<br />

static controls to display text, and “OK”, “Cancel” and “Help” buttons. The following table lists their IDs<br />

and functions:<br />

Function ID symbol ID Value<br />

Static control used to display directory stc1 1088<br />

Static control contains string “List Files of Type” stc2 1089<br />

Static control contains string “Folder” stc3 1090<br />

Static control contains string “Drive” stc4 1091<br />

Edit box used to input file name edt1 1152<br />

List box used to display files lst1 1120<br />

List box used to display directories lst2 1121<br />

Combo box used to display file types cmb1 1136<br />

Combo box used to display drives cmb2 1137<br />

Static control contains string “Directories” not defined 65535<br />

“Read only” check box chx1 1040<br />

OK button<br />

IDOK<br />

Cancel button<br />

IDCANCEL<br />

Help button pshHelp 1038<br />

Figure 7-2 shows the positions of these controls.<br />

stc3<br />

edt1<br />

lst1<br />

stc1<br />

lst2<br />

IDOK<br />

IDCANCEL<br />

stc2<br />

stc4<br />

pshHelp<br />

cmb1<br />

cmb2<br />

chx1<br />

Figure 7-2. Standard file dialog template<br />

We must design a dialog template that contains exactly the same controls in order to replace the default<br />

template with it. This means that the custom dialog template must have static controls with IDs of 1088,<br />

1089, 1090..., list boxes with IDs of 1120 and 1121, combo boxes with IDs of 1136 and 1137, and so on.<br />

178

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

Saved successfully!

Ooh no, something went wrong!