FILE: Directory Picker Sample Using Modified CFileDialog

Last reviewed: January 15, 1998
Article ID: Q105497

The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1

SUMMARY

The default implementation of CFileDialog allows the user to select a specific file from any drive/directory on the computer. It is possible to derive a class from CFileDialog, creating a new class that allows the user to select only a directory. In addition to deriving a new class, this approach also requires that a custom template be used in conjunction with the new dialog box. The Directory Picker (DIRPK) sample implements this functionality.

The following file is available for download from the Microsoft Software Library:

 ~ Dirpk.exe (size: 79675 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from
               Online Services

DIRPK.EXE was archived as a self-extracting file using the PKware file- compression utility. The archived file contains subdirectories, and therefore, the -d switch needs to be used when decompressing the file to disk.

MORE INFORMATION

NOTE: The DIRPK sample was designed using the old style of common file dialogs and will work as intended only on Windows NT 3.51. On Windows 95 and future versions of NT (4.0), if you want to use the old style of common file dialogs, insure that:

   m_ofn.Flags &= ~OFN_EXPLORER;

before calling DoModal() on the CFileDialog object. It is likely, however, that this behavior will change under future versions of Windows and the old style of common file dialogs will go away. The best way to search for a folder instead of a specific file is to use the new ::SHBrowseForFolder() SDK function.

The following steps use the DIRPK sample to derive a class from CFileDialog which allows the user to select only a directory:

  1. Derive a class from CFileDialog. See the DIRPK sample for the specific implementation.

  2. Modify the default CFileDialog dialog box template to suit the needs of your dialog box. When modifying a default template, be sure to leave all existing controls there--you can move them around, but they must exist. (COMMDLG.DLL expects certain controls to exist in the template.) Of course, you can add new controls if desired. The default dialog box templates can be found in the \MSVC\SAMPLES\COMMDLG directory in Visual C++ for Windows. In Visual C++ 32-bit edition, the templates are found in \MSVC\INCLUDE. Each template has a .DLG file extension.

  3. Include the modified template in your application's .RC file. Note that the default dialog box templates contain constant identifiers such as cmb1, lst1, stc1, and so forth. These constants are defined in the DLGS.H file, located in the \\MSVC\INCLUDE directory. You must include this file in your application's .RC file as well. Using App Studio, choose Set Includes from the File menu and enter
       #include "dlgs.h" in the Read-Only Symbol Directives edit control.
    
    

  4. Add any necessary member functions. See the DIRPK sample for implementation details.

  5. Create an object of your new class and call the DoModal function to bring up the dialog box. Code similar to the following should be used:

Sample Code

   CMyFileDlg  cfdlg(FALSE, NULL, NULL, OFN_SHOWHELP | OFN_HIDEREADONLY |
                     OFN_OVERWRITEPROMPT | OFN_ENABLETEMPLATE, NULL,
                     m_pMainWnd);
   cfdlg.m_ofn.hInstance = AfxGetInstanceHandle();
   cfdlg.m_ofn.lpTemplateName = MAKEINTRESOURCE(FILEOPENORD);
   if (IDOK==cfdlg.DoModal())
       ...OK processing...
   else
       ...Error processing...


Additional query words: getopenfilename commdlg dirpkr dirpick dirpicker
dirpik
Keywords : MfcFileIO
Technology : kbMfc
Version : 1.0 1.5 1.51 1.52 2.0
Platform : NT WINDOWS
Issue type : kbfile
Solution Type : kbfile kbsample


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.