Dialog for Selecting Directories

Friday Jan 14th 2000 by Andrew Phillips

Dialog for Selecting Directories


The CDirDialog class is an extension to MFC that allows selection of a directory with several nice user-interface features.

The class is derived from the MFC CFileDialog class. The code was built and tested with VC6 but should work with recent earlier versions of MFC, and probaly later ones too.

What it Does

The Microsoft recommended way to select a directory (folder) is to use SHBrowseForFolder. This facility is rather primitive for many reasons. For example, there is no way to create or delete a directory, view the files in the directory etc. As CFileDialog (and hence CDirDialog) uses the standard file open dialog you can do these things and more. One thing I really like is that the dialog is resizable under Windows 95/98 so you can expand it to see a lot more files/directories.

Since writing this I have found there is similar code on CodeGuru that provides a directory selection facility using CFileDialog. I think this one is better as it avoids a few problems and bugs and has the following extra features:

  • The file display area shows the contents of the current folder as the user types a full path. (Updated when a backslash or Enter is typed.)
  • Directory name completion means that if the characters type are the first characters of an existing directory then the rest of the directory name is displayed so the user can just press Enter. The extra characters are displayed selected so the user can easily override them if desired.
  • If a directory does not exist the user has the otpion of creating it. This will create all ancestor directories that do not exist.
  • The user has the option of toggling the display of files. By default only directories are displayed but the user can select to display different type of files from the "Display Files of Type" drop down list. (This is unlike the others where the display of files can be turned on or off only under programmer, not user, control.)
  • If files are displayed the user cannot select one.
  • Double-clicking a directory name does not select the directory but simply changes the display to show the contents of the directory. To actually select the directory the user must click the Open button.
  • Helpful error messages that explain why a drive name or path is invalid.

How to Use

To use the class just copy and include the DirDialog.h and DirDialog.cpp files into your project. You also need to link with IMAGEHLP.LIB as the standard Windows IMAGEHLP.DLL is called.

To use the dialog create an object of type CDirDialog and call its DoModal member function. In the same way you would for CFileDialog. The constructor takes 3 parameters: the initial directory, a set of file filters (as is often supplied to the CFileDialog constructor) and a pointer to the dialog's parent window. These parameters are optional and default to the current directory, displaying either all files (*.*) or no files (folders only), and no parent window.

The demo project shows how to use the CDirDialog class. The code for the class is included in the demo project. The demo executable is in the Release directory.

See DirDialog.cpp for more info.


Download demo and source code project - 27 Kb
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved