Click here to Skip to main content
11,429,154 members (63,031 online)
Click here to Skip to main content

CFolderDialog - Selecting Folders

, 17 Feb 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
The CFolderDialog class allows you to add a folder-selection dialog box to your applications.

Introduction

As I mentioned in my other article "CIconDialog - Selecting Icons", while developing a wizard application, I needed a dialog to select an icon from executables and another one for selecting folders on multiple ones, but did not find anything about in MFC. So, CFolderDialog was written. It wraps the ::SHBrowseForFolder API.

Sample Usage

Sample Image

The CFolderDialog is derived from CCommonDialog and acts like any common dialog. See sample usage:

//...
#ifndef __FOLDERDLG_H__
    #include "FolderDlg.h"
#endif
// ...
void CSomeDialog::OnSomeHandler( void ) 
{ 
    m_strFolderPath = _T( "d:\\Windows" ); // Just for sample    
    m_strDisplayName.Empty();
    
    CFolderDialog dlg(  _T( "Dialog Title" ), m_strFolderPath, this );
    if( dlg.DoModal() == IDOK  )
    {    
        m_strFolderPath  = dlg.GetFolderPath();
        m_strDisplayName = dlg.GetFolderDisplayName();
        // Use folder path and display name here ...
    }    
}
//

Sample Image

See demo project source for more.

Setting the Root Folder

You can also set the root folder of the dialog, specifying the location of the root folder from which to start browsing. Only the specified folder and any subfolders that are beneath it in the namespace hierarchy will appear in the dialog box.

Sample Image

See sample usage:

//...
#ifndef __FOLDERDLG_H__
    #include "FolderDlg.h"
#endif
// ...
void CSomeDialog::OnSomeHandler( void ) 
{
    CFolderDialog dlg( _T( "Root folder is C:\" ), NULL, this );
    dlg.SetRootFolder( _T( "C:\\" );
    
    if( dlg.DoModal() == IDOK  )
    {       
        // ...
    }
}
//

Sample Image

Thanks to Eckhard Schwabe and Jose Insa for that sample.

Custom Filtering

Under Microsoft® Windows® XP/2003 or later, you can do custom filtering on the contents of the dialog box.

Sample Image

To create a custom filter, follow these steps:

  1. Set the BIF_NEWDIALOGSTYLE flag in the uFlags member of the CFolderDialog constructor. Override the OnIUnknown virtual member function in the derived class. On OnIUnknown, the function's pIUnknown parameter will contain a pointer to an instance of IUnknown. Call QueryInterface on that IUnknown to obtain a pointer to an IFolderFilterSite.
  2. Create an object that implements IFolderFilter - derive a class from it that implements all basic pure virtual member functions of IUnknown, and implement IFolderFilterSite::ShouldShow and IFolderFilterSite::GetEnumFlags functions, that do filtering.
  3. Call IFolderFilterSite::SetFilter (pointer to which you obtained in step 1), passing it a pointer to your custom IFolderFilter derived class. IFolderFilterSite::ShouldShow and IFolderFilterSite::GetEnumFlags methods can then be used to include and exclude items from the tree.
  4. Once the filter is created, the IFolderFilterSite interface is no longer needed. Call IFolderFilterSite::Release if you have no further use for it.

Sample Image

I have added a sample custom filtering (look at the picture, the dialog shows only "JPG/GIF/BMP" files in the tree). Thanks to Arik Poznanski for his article "C# does Shell, Part 1". For more information, please see the source code.

Class Members

Base Class

  • CCommonDialog

Data Members

  • m_bi - The Windows BROWSEINFO structure. Provides access to basic folder dialog box parameters.
  • m_szFolPath - Contains the path of the folder selected with the dialog.
  • m_szSelPath - Contains the folder path to be initially selected when the the dialog opens.

Construction

Constructs a CFolderDialog object:

CFolderDialog( IN LPCTSTR pszTitle = NULL, IN LPCTSTR pszSelPath = 
      NULL, IN CWnd* pWndParent = NULL, IN UINT uFlags = BIF_RETURNONLYFSDIRS )
  • pszTitle - Title to display in the top of the dialog.
  • pszSelPath - The folder path to be initially selected when the the dialog opens.
  • pWndParent - A pointer to the file dialog-box object's parent or owner window.
  • uFlags - A combination of one or more flags that allows you to customize the dialog box. For more information, see BROWSEINFO structure in the Platform SDK.

Operations

  • DoModal( void ) Displays the browse for folder dialog box and allows the user to make a selection.
  • SetSelectedFolder( IN LPCTSTR pszPath ) - Sets the folder path to be initially selected when the the dialog opens.
  • SetRootFolder( IN LPCTSTR pszPath ) - Sets the root folder path to show. If pszPath is NULL, the root folder is removed.
  • GetRootFolder( IN OUT LPTSTR pszPath ) - Gets the root folder of the dialog. The pszPath buffer must be at least MAX_PATH characters in size.
  • GetSelectedFolder( void ) const - Gets the folder path to be initially selected when the the dialog opens.
  • GetFolderPath( void )const - Retrieves the path of the open folder.
  • GetFolderDisplayName( void )const - Retrieves the display name of the currently open folder.
  • GetFolderImageIndex( void )const - Gets the image associated with the selected folder. The image is specified as an index to the system image list.
  • GetBI( void ) - Retrieves the BROWSEINFO structure of the CFolderDialog object.
  • GetBI( void )const - Retrieves the BROWSEINFO structure of the CFolderDialog object.

Overridables

  • OnInitialized( void ) - Called when browse dialog box has finished initializing.
  • OnSelChanged( IN LPITEMIDLIST pItemIDList ) - Called when browse dialog box selection is changed.
  • OnValidateFailed( IN LPCTSTR pszPath ) - Called when the user typed an invalid name into the edit box (if any) of the browse dialog box. Return zero to allow the dialog to be dismissed or nonzero to keep the dialog open.

Microsoft® Windows® XP/2003 or later:

  • OnIUnknown( IN IUnknown* pIUnknown ) - Provides an IUnknown interface to the client for custom filtering of the contents of the dialog box, using IFolderFilterSite and IFolderFilter.

Functions, that are valid to be called only from Overridables:

  • EnableOK( IN BOOL bEnable = TRUE ) - Enables or disables the browse dialog box's OK button.
  • SetSelection( IN LPITEMIDLIST pItemIDList ) - Selects the specified folder.
  • SetSelection( IN LPCTSTR pszPath ) - Selects the specified folder.
  • SetStatusText( IN LPCTSTR pszText ) - Sets the dialog box status text.

Shell version 5.0 or later:

  • SetExpanded( IN LPITEMIDLIST pItemIDList ) - Specifies a path to expand in the dialog box.
  • SetExpanded( IN LPCTSTR pszPath ) - Specifies a path to expand in the dialog box.
  • SetOKText( IN LPCTSTR pszText ) - Sets the dialog box "OK" button text.

Notes

If you convert this project to VC 7.0 or later, do not forget to add shlwapi.dll to the list of delay loaded DLLs because it uses ::StrRetToStr function of shlwapi.dll 5.0. Go to "[Project Properties]>[Configuration Properties]>[Linker]>[Input]" and add "shlwapi.dll;" to the list of "[Delay Loaded DLLs]". VC 6.0 project does this using "/DelayLoad" linker option, not supported under VC 7.0 or later.

Version History

  • 27 Mar 2002
    • Posted the article.
  • 30 Mar 2003
    • Some code changes.
    • Added missing in old Platform SDK new flag definitions.
    • Added support for both MFC 6.0 and 7.0.
    • Added OnIUnknown handler for Microsoft® Windows® XP folder filtration.
    • Added SetExpanded, SetOKText and GetSelectedFolder functions.
  • 30 May 2003
    • Added OnSelChanged default implementation.
  • 14 Jul 2003
    • Added custom filtering sample for Microsoft® Windows® XP/2003 or later.
    • Set SetExpanded and SetOKText to noinline.
  • 07 Jan 2004
    • Added SetRootFolder and GetRootFolder functions.
    • Changed IFolderFilter implementation.
  • 15 Feb 2005
    • Small bug fix in DoModal, thanks to WindSeven.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Armen Hakobyan
Software Developer (Senior) SafeNet Inc
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
JunfengGuo19-Jan-14 21:46
memberJunfengGuo19-Jan-14 21:46 
Questioncann't edit resouce file in vs2008 Pin
oskycar19-Oct-13 5:24
memberoskycar19-Oct-13 5:24 
GeneralMy vote of 5 Pin
Joseph Masters10-Jul-13 7:58
memberJoseph Masters10-Jul-13 7:58 
GeneralMy vote of 5 Pin
om14-Mar-13 7:01
memberom14-Mar-13 7:01 
QuestionFull path in the Edit box [modified] Pin
Kranggg4-Mar-13 3:23
memberKranggg4-Mar-13 3:23 
GeneralMy vote of 5 Pin
Kranggg4-Mar-13 3:22
memberKranggg4-Mar-13 3:22 
Questioncan't see folder for my android device [modified] Pin
steveh211216-Feb-13 23:15
membersteveh211216-Feb-13 23:15 
QuestionCode Pin
luizalves197216-Mar-12 10:48
memberluizalves197216-Mar-12 10:48 
QuestionFolder Dialog Pin
joh meerholz12-Aug-11 3:01
memberjoh meerholz12-Aug-11 3:01 
GeneralError when built in VS 6.0 Pin
JamesBunger29-Sep-10 10:58
memberJamesBunger29-Sep-10 10:58 
GeneralRe: Error when built in VS 6.0 Pin
Armen Hakobyan29-Sep-10 12:02
memberArmen Hakobyan29-Sep-10 12:02 
GeneralRe: Error when built in VS 6.0 Pin
Armen Hakobyan29-Sep-10 12:08
memberArmen Hakobyan29-Sep-10 12:08 
QuestionVisual Studio 6 Pin
JamesBunger28-Sep-10 16:33
memberJamesBunger28-Sep-10 16:33 
AnswerRe: Visual Studio 6 Pin
Armen Hakobyan29-Sep-10 8:37
memberArmen Hakobyan29-Sep-10 8:37 
GeneralAvoid SHBrowseForFolder Pin
Andrew Phillips3-Sep-10 1:27
memberAndrew Phillips3-Sep-10 1:27 
GeneralRe: Avoid SHBrowseForFolder Pin
gordon8816-Jun-12 13:53
membergordon8816-Jun-12 13:53 
QuestionHow can insert the user and password for some network patth Pin
luiggivillar30-Apr-10 9:45
memberluiggivillar30-Apr-10 9:45 
GeneralBug If initial folder can not be reached. [modified] Pin
Member 305768817-Jan-10 22:42
memberMember 305768817-Jan-10 22:42 
GeneralRe: Bug If initial folder can not be reached. Pin
Armen Hakobyan19-Jan-10 4:14
memberArmen Hakobyan19-Jan-10 4:14 
GeneralLinker Errors Pin
Anur Ravi30-Dec-09 8:52
memberAnur Ravi30-Dec-09 8:52 
QuestionHow to install filter for explorer? Pin
nanfang30-Dec-07 4:14
membernanfang30-Dec-07 4:14 
Questionhow to use custom filter Pin
EdwardChan10-Oct-07 15:53
memberEdwardChan10-Oct-07 15:53 
GeneralAuto start transfer from camera (MTP) to disk Pin
andygm27-Jul-07 0:36
memberandygm27-Jul-07 0:36 
QuestionLicense Pin
wjvdh10-Jul-07 1:49
memberwjvdh10-Jul-07 1:49 
AnswerRe: License Pin
Armen Hakobyan10-Jul-07 2:28
memberArmen Hakobyan10-Jul-07 2:28 
GeneralRe: License Pin
wjvdh10-Jul-07 2:34
memberwjvdh10-Jul-07 2:34 
QuestionStrRetToStr undeclared identifier Pin
ddgsptntea5-Jul-07 1:00
memberddgsptntea5-Jul-07 1:00 
AnswerRe: StrRetToStr undeclared identifier Pin
Armen Hakobyan6-Jul-07 1:24
memberArmen Hakobyan6-Jul-07 1:24 
QuestionHow to compile in Visual C++ 6.0 Pin
cristitomi25-Mar-07 23:47
membercristitomi25-Mar-07 23:47 
AnswerRe: How to compile in Visual C++ 6.0 Pin
Armen Hakobyan29-Mar-07 6:59
memberArmen Hakobyan29-Mar-07 6:59 
GeneralRe: How to compile in Visual C++ 6.0 Pin
cristitomi29-Mar-07 11:38
membercristitomi29-Mar-07 11:38 
GeneralHidding 'CWnd::m_hWnd' .. Pin
rbid4-Dec-06 0:54
memberrbid4-Dec-06 0:54 
GeneralOn folder's content: enable/disable the Ok bttn Pin
Le cosaque12-Oct-06 3:00
memberLe cosaque12-Oct-06 3:00 
AnswerRe: On folder's content: enable/disable the Ok bttn Pin
Armen Hakobyan18-Oct-06 5:36
memberArmen Hakobyan18-Oct-06 5:36 
QuestionScroll flickering Pin
ShiriA24-Jul-06 8:25
memberShiriA24-Jul-06 8:25 
Questionhow about MTP (Media Transport Protocol) supported device Pin
decang11-Nov-05 5:38
memberdecang11-Nov-05 5:38 
QuestionHow to get the folder path of "My Network Places" on SelChanged Pin
Naru21-Oct-05 15:47
memberNaru21-Oct-05 15:47 
AnswerRe: How to get the folder path of "My Network Places" on SelChanged Pin
Naru22-Oct-05 7:53
memberNaru22-Oct-05 7:53 
GeneralAdaptation for a Matlab DLL Pin
ohad gal27-Aug-05 23:37
sussohad gal27-Aug-05 23:37 
AnswerRe: Adaptation for a Matlab DLL Pin
Armen Hakobyan29-Aug-05 0:52
memberArmen Hakobyan29-Aug-05 0:52 
GeneralRe: Adaptation for a Matlab DLL Pin
Ohad Gal31-Aug-05 14:25
sussOhad Gal31-Aug-05 14:25 
GeneralI dont see the "New Folder" button Pin
Atewa8-Apr-05 13:07
memberAtewa8-Apr-05 13:07 
GeneralRe: I dont see the "New Folder" button Pin
Steve Mayfield8-Apr-05 14:07
memberSteve Mayfield8-Apr-05 14:07 
GeneralRe: I dont see the "New Folder" button Pin
Darren Hutchinson30-Jan-06 17:51
memberDarren Hutchinson30-Jan-06 17:51 
GeneralRe: I dont see the "New Folder" button Pin
sunnyloves31-May-10 22:39
membersunnyloves31-May-10 22:39 
Generalcompile errors Pin
TomDuffy1-Apr-05 10:57
memberTomDuffy1-Apr-05 10:57 
GeneralRe: compile errors Pin
Armen Hakobyan4-Apr-05 0:53
memberArmen Hakobyan4-Apr-05 0:53 
GeneralRe: compile errors Pin
TomDuffy4-Apr-05 8:46
memberTomDuffy4-Apr-05 8:46 
GeneralFile dialog to te camera Pin
Alex Evans13-Feb-05 19:29
memberAlex Evans13-Feb-05 19:29 
GeneralRe: File dialog to te camera Pin
Armen Hakobyan13-Feb-05 20:57
memberArmen Hakobyan13-Feb-05 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 17 Feb 2005
Article Copyright 2002 by Armen Hakobyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid