You are on page 1of 9

Using the CFileDialog Class

The CFileDialog class is a common-dialog box class. This means that the dialog box is pre-made, and is built into the language. You may have noticed that most Save and Open Dialog boxes in Windows look exactly the same. This is because they all use the Common Dialog Box for open and save. This is the CFileDialog class. The dialog is useful in many different ways. Not only is it easier than coding the functions and dialogs yourself, the Common Dialog Boxes have built in functions, such as Create Folder and Delete File. Construction of a CFileDialog. To begin using the CFileDialog you must first create one just as you would any other variable, with this line:
CFileDialog dlgOpenBox;

This creates a CFileDialog box called dlgOpenBox, which can be manipulated like any other class. But this code wont work just yet, the CFileDialog has a long list of parameters that need to be included, here is an example of a line of code for the CFileDialog when complete.
CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

This code creates a CFileDialog with various options created. Lets take a look at the various parameters necessary for the CFileDialog to be initialized.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

The first value is a bool, either true or false. If marked as True the box will be an Open Dialog Box, if marked False the box will be created as a Save-As Dialog Box.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

The next parameter is the default extension. In an Open Dialog Box, the default extension is the default extension the program will attempt to load. For example, if it is marked as *.mp3 (With the quotes) the Open Dialog box will only show MP3 files. In a Save-As dialog box, the default extension is automatically appended to the file unless otherwise specified. If there is no default extension, mark the field NULL.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

This parameter operates similar to the default extension parameter. The file specified, written with this syntax : TheFile.Ext, will be the only file the Open Dialog Box shows, and the file name the Save-As dialog box automatically saves the file as.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

The next parameter allows customization over the CFileDialog. In the example, the flag specified makes the Save-As Dialog Box ask the user before overwriting a file. There are a total 26 possible flags, and they may be added in combination with each other. For a complete listing check Appendix A. To separate multiple flags use the | character.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

This parameter specifies a filter for the dialog box. When used in an Open-Dialog Box, the box will restrict what type of files the user can browse and open in the box to the contents of the filter. The filter is a String array, and you enter the file name of the array as the parameter. An example of a filter is :
static char BASED_CODE Filter[]= "Playlists (*.ply,*.m3u)|*.ply;*.m3u||";

This filter will only show Playlist files with the extension of ply or m3u. The first part of the filter, including the title and the extensions listed in Parentheses is the definition text. The text that will show in a drop down box where the user selects what filter to use. The extensions listed in between the | characters is the actual extension the computer uses. For one filter to allow multiple extensions, such as both ply and m3u files, separate the extensions with a semi-colon. The second | denotes the end of the entire filter. To have a filter with two filters that the user can choose between, use the following syntax:
static char BASED_CODE szFilter[] = "Playlists (*.ply)|*.ply|Winamp Playlists (*.m3u)|*.m3u||";

This code allows the user to specify which filter to use by use of a drop-down box built into the Common Dialog Control.

CfileDialog dlgOpen(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,szFilter,NULL);

The final parameter is a pointer to the owner window of the Dialog Box. Simply stating this as Null refers it to the DialogBox that called it. Using the CFileDialog. Now that the CFileDialog box, dlgOpen is created, it can be used. First you need to call the DialogBox up by using the DoModal function. When using a CFileDialog an error-trap on the Do Modal is necessary. For example, whenever you use the dlgOpen.DoModal command, you need to include the next actions in an if statement in order to keep the actions from being carried out when the user selects cancel. For example:
If (dlgOpen.DoModal==IDOK) { //USE The Data returned in here }

This code will commence the action only when the user has selected OK. To commence action when the user has selected Cancel check to see if dlg.Modal() == IDCANCEL. Now the dialog boxs is gone, but the program doesnt know what file you selected. Getting Data from a CFileDialog. The function necessary is the GetPathName and its similar functions, GetFileName, GetFileExt, and GetFileTitle. All four of these functions operate in the same manner, the key difference is in what value they return. All four are CString functions, and therefore must be assigned to a CString.
CString TheString;

TheString = dlgOpen.GetFileName();

This will retrieve the File Name, for example, Word.Doc, from the dialog. Using these four functions effectively is necessary for achieving the desired results. GetPathName will retrieve the entire path, i.e. C:\Music\Song.Mp3. GetFileName retrieves simply the name of the file, Song.mp3. GetFileExt retrieves the extension of the file and in the example would equal mp3. Finally GetFileTitle retrieves the name of the file without the extension, for example Song. Using these four functions you get all the information required for finding or opening the file. The CFileDialog could be used effectively using only the afore-mentioned functions. However, the CFileDialog also has two other functions useful in operating efficiently. Retrieving Selected File Information One such function is the GetReadOnlyPref is a Boolean function that simply checks to see if the file is read-only. If the file is read only, the file cannot be edited, thus you can error-trap for files unable to be written to before you write using this function. The function returns True if the file is read only, False if the file is not read only. Another function of the CFileDialog class is the GetStartPosition function. This function is a POSITION value and returns the position of the of the first file pathname in the list if OFN_ALLOWMULTISELECT is enabled. If this is unavailable, the function returns Null. The CFileDialog is a useful tool in acquiring the data about what file the user wants to open, or the filename of the file they want to save. This data can be coupled with the CFile class in order to manipulate simple files.

Appendix A: Index of OFN Flags for the CfileDialog Class

1) OFN_MULTISELECT This flag allows the user to select multiple files in an Open Dialog Box. The list of files selected if multiple files are selected are stored in the the buffer lpstrFile.

2) OFN_CREATEPROMPT This flag is used if the user selects a file that doesnt exist. When this flag is specified the dialog will ask the user if they want to create the file.

3) OFN_DONTADDTORECENT When specified, this flag prevents a link to the file selected on the documents menu located on the start menu.

4) OFN_ENABLEHOOK Enables the hook specified in the lpfnHook function.

5) OFN_ENABLEINCLUDENOTIFY A Windows 2000 only function, it sends notifications to the hook, which can be further modified to what is shown in the dialog boxs file list.

6) OFN_ENABLESIZING This particular flag allows the user to resize both the open and save as dialog boxes. It only applies in a Windows 98 environment.

7) OFN_ENABLETEMPLATE Can be used in two different methods. It can be coupled with the OFN_EXPLORER flag to specify a template for the CFileDialog. If the OFN_EXPLORER flag is not activated than the dialog uses the old-style interface.

8) OFN_EXPLORER This flag is not necessary in most circumstances since the dialog automatically uses the explorer interface. However it is necessary when making an alteration to the way the dialog box works. For example, if using the Mutliselect flag, you must also use this flag for it to operate properly.

9) OFN_EXTENSIONDIFFERENT This particular flag indicates that the extension selected is different from the default extension specified in the declaration of the CFileDialog class.

10) OFN_FILEMUSTEXIST This flag is used to keep the user from selecting a file that doesnt exist. The OFN_PATHMUSTEXIST must be used in conjunction with this flag.

11) OFN_FORCESHOWHIDDEN A flag only used in a Windows 2000 environment. When specified the CFileDialog will show hidden files no matter what the users specified setting is.

12) OFN_HIDEREADONLY This flag hides the read-only checkbox on the dialog.

13) OFN_NOCHANGEDIR Restores the original directory names and structure if the user changes it while navigating the CFileDialog.

14) OFN_NODEREFERENCELINKS If the user selects a shortcut or link (*.lnk), the CFileDialog returns the path of the link, rather than following the link.

15) OFN_NOLONGNAMES If the CFileDialog is explorer style, this flag has no effect. If its not an explorer style CFileDialog, then the user is only allowed to specify files names 8 characters or less.

16) OFN_NONETWORKBUTTON Hides the network button in the CFileDialog.

17) OFN_NOREADONLYRETURN Tells the computer that the file is not read only, no matter whether it is read only or not.

18) OFN_NOTESTFILECREATE When used, the computer doesnt check for hard disk space, read-write protection or if the file exists before creating the file. This can be used if you dont want files modified, and all additions are separate files.

19) OFN_NOVALIDATE Allows invalid characters to be entered as the file name in a CFileDialog dialog.

20) OFN_OVERWRITEPROMPT Prompts the user if the file already exists, giving them the option to overwrite the selected file or not.

21) OFN_PATHMUSTEXIST Only allows the user to specify directory structures and files that exist. If it doesnt exist, then the CFileDialog dialog will alert the user with a warning box.

22) OFN_READONLY Automatically selects the read-only checkbox upon the creation of the CFileDialog dialog.

23) OFN_SHAREAWARE If this flag is used, the CFileDialog will override any Sharing Violation errors and returns the file name. This can be useful if the program isnt going to write any data to the file or read any data, simply use the file information.

24) OFN_SHOWHELP Makes the CFileDialog dialog show a help button.

You might also like