MFC Wrapper for SHBrowseForFolder

Copyright ) 1998-1999, Kenneth M. Reed, All rights


CFolderDialog is an MFC based class that
encapsulates the Windows Shell function for browsing folders, ::ShBrowseForFolder.

To use it, you create an instance of CFolderDialog
and call the DoModal method. Upon return, the method
GetPathName can be used to retrieve the path to the
user selected folder.

The constructor allows you to specify an optional initial
folder, flags specific to the shell function (see the description
of the ulFlags field of the BROWSEINFO structure
in the help for Visual Studio or the Windows Shell API) and a
pointer to the parent window’s CWnd. All of
these arguments are optional.

You may override the two virtual methods OnInitDialog
and OnSelChanged in a derived class to customize
their behavior. The default behavior of OnInitDialog
is to call SetSelection and SetStatusText
with the folder name provided in the constructor. The default
version of OnSelChanged places the new path into the
status text area of the dialog.

The class provides utility functions for 1) enabling or
disabling the OK button (EnableOK), 2) setting the
status text that appears above the folder list box in the browser
folder dialog box (SetStatusText) and 3) setting the
current selection (SetSelection). The file MyFolderDialog.h,
included in the zip file, shows an example of a derived class
which uses SetStatusText.

For advanced users, the BROWSEINFO structure is
available via the m_bi member variable. This
structure is initialized by the constructor and any changes
should be made before the call to DoModal. See the help for
Visual Studio or the Windows Shell API for more information on
the BROWSEINFO structure.

The zip file also contains the source for CMyFolderDialog, a
class derived from CFolderDialog to show how to take advantage of
some of the advanced features.


December 29, 1998
Added the CMyFolderDialog source code the version
available on the codeguru site.
June 23, 1998
Fixed a problem in EnableOK. The documenation for the
SHBrowseForFolder call back incorrectly states that it is
the wParam on the BFFM_ENABLEOK message which controls
the enabling/disabling of the OK button. However, It is
actually the lParam.
Thanks to Langis Pitre for finding this one.
April 5, 1998
Changed the way status text is handled:

  • The constructor now forces the BIF_STATUSTEXT
    flag to be set
  • Changed the base OnInitDialog method
    to display the initial folder in the status text.
  • Changed the base OnSelChanged method
    to update the contents of m_strFinalFolderName
    and display the new folder in the status text
  • Removed the MyFolderDialog.h file
    from the zip.

Bugs and Suggestions

Please send suggestions and/or bugs to

Ken Reed

Download source code – 6 KB

Date Last Updated: February 3, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read