CFolderDialog - Selecting Folders






4.95/5 (87 votes)
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
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 ... } } //
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.
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 ) { // ... } } //
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.
To create a custom filter, follow these steps:
- Set the
BIF_NEWDIALOGSTYLE
flag in theuFlags
member of theCFolderDialog
constructor. Override theOnIUnknown
virtual member function in the derived class. OnOnIUnknown
, the function'spIUnknown
parameter will contain a pointer to an instance ofIUnknown
. CallQueryInterface
on thatIUnknown
to obtain a pointer to anIFolderFilterSite
. - Create an object that implements
IFolderFilter
- derive a class from it that implements all basic pure virtual member functions ofIUnknown
, and implementIFolderFilterSite::ShouldShow
andIFolderFilterSite::GetEnumFlags
functions, that do filtering. - Call
IFolderFilterSite::SetFilter
(pointer to which you obtained in step 1), passing it a pointer to your customIFolderFilter
derived class.IFolderFilterSite::ShouldShow
andIFolderFilterSite::GetEnumFlags
methods can then be used to include and exclude items from the tree. - Once the filter is created, the
IFolderFilterSite
interface is no longer needed. CallIFolderFilterSite::Release
if you have no further use for it.
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 WindowsBROWSEINFO
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, seeBROWSEINFO
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. IfpszPath
isNULL
, the root folder is removed.GetRootFolder( IN OUT LPTSTR pszPath )
- Gets the root folder of the dialog. ThepszPath
buffer must be at leastMAX_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 theBROWSEINFO
structure of theCFolderDialog
object.GetBI( void )const
- Retrieves theBROWSEINFO
structure of theCFolderDialog
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 anIUnknown
interface to the client for custom filtering of the contents of the dialog box, usingIFolderFilterSite
andIFolderFilter
.
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
andGetSelectedFolder
functions.
- 30 May 2003
- Added
OnSelChanged
default implementation.
- Added
- 14 Jul 2003
- Added custom filtering sample for Microsoft® Windows® XP/2003 or later.
- Set
SetExpanded
andSetOKText
tonoinline
.
- 07 Jan 2004
- Added
SetRootFolder
andGetRootFolder
functions. - Changed
IFolderFilter
implementation.
- Added
- 15 Feb 2005
- Small bug fix in
DoModal
, thanks to WindSeven.
- Small bug fix in