Virtual Developer Workshop: Containerized Development with Docker

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


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 area.
  • Removed the MyFolderDialog.h file from the zip.

Bugs and Suggestions

Please send suggestions and/or bugs to reedk@daneel.mv.com.

Ken Reed

Download source code - 6 KB

Date Last Updated: February 3, 1999


  • Thanks, works great.

    Posted by porphyrula on 03/07/2007 11:20pm

    Thanks, you saved me a lot of time and bother.

  • exactly what i needed, but...

    Posted by mensch on 09/15/2005 06:09am

    u might want to add this in somewhere if you want to pause the application while browsing, modify the m_bi.hwndOwner value to a non-zero value, then call domodal

  • Nifty!

    Posted by Legacy on 06/08/2003 07:00am

    Originally posted by: Mohamed Mustafa

    i used this in 2k, it soves the purpose. thanx

  • Memory leak somewhere :((

    Posted by Legacy on 04/03/2003 08:00am

    Originally posted by: Dmitry

    This is what I got from VS.NET:

    Detected memory leaks!
    Dumping objects ->
    {97} normal block at 0x00A971D8, 20 bytes long.
    Data: < ~^ > 08 7E 5E 00 03 00 00 00 03 00 00 00 01 00 00 00
    {96} normal block at 0x00A97188, 20 bytes long.
    Data: < ~^ > 08 7E 5E 00 03 00 00 00 03 00 00 00 01 00 00 00
    {95} normal block at 0x00A96F10, 568 bytes long.
    Data: <4 Y <o > 34 B0 59 00 94 04 0B 00 00 00 00 00 3C 6F A9 00
    Object dump complete.

    Any idea what the reason of leaks?


  • Good Class

    Posted by Legacy on 09/25/2001 07:00am

    Originally posted by: LBD

    Very useful class. Is it free to the general public for use in applications as long as the copyright notices are maintained?

  • THX a lot

    Posted by Legacy on 09/17/2001 07:00am

    Originally posted by: Jan

    Thx for this class.
    I've spent days with searching an object of MFC to do this.

    Unbelievable that something like that isn't included to MFC.

  • Browse for folder

    Posted by Legacy on 09/08/2001 07:00am

    Originally posted by: Johnny Olsen

    Thanks for your code.
    I've already made the same in Delphi some years ago,
    but now I needed it in Visual C++.
    Of course I was possible for me to copy the functionality,
    from the Delphi code, but it is somewhat faster, just to
    include your classes directly in the application.

    Kind regards

  • please help !!

    Posted by Legacy on 04/20/2001 07:00am

    Originally posted by: Baba

    I find this wrapper very interesting...but I want to display both folders and files.
    I tried the use of the BIF_BROWSEINCLUDEFILES flag but it don't work!!

    Do any one get an idea for me? thanks!

  • Another feature -> Function to set the root path

    Posted by Legacy on 02/22/2000 08:00am

    Originally posted by: Jose Insa

    First, thanks to Kenneth for the class.

    This function will set as root the folder lpszRootFolder.
    You must call the function before DoModal();

    I have adapted it from Microsoft knowledge Base Article
    ID Q132750: Convert a File Path to an ITEMIDLIST

    bool CFolderDialog::SetRootFolder(LPCTSTR lpszRootFolder)
    LPSHELLFOLDER pDesktopFolder;
    OLECHAR olePath[MAX_PATH];
    ULONG chEaten;
    ULONG dwAttributes;
    HRESULT hr;

    // Get a pointer to the Desktop's IShellFolder interface.
    if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))

    // IShellFolder::ParseDisplayName requires the file name be in
    // Unicode.
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszRootFolder, -1,
    olePath, MAX_PATH);

    // Convert the path to an ITEMIDLIST.
    hr = pDesktopFolder->ParseDisplayName(NULL,
    if (FAILED(hr))
    m_bi.pidlRoot = NULL;
    return false;// Handle error.

    m_bi.pidlRoot = pidl;
    //release the desktop folder object
    return true;

    m_bi.pidlRoot = NULL;
    return false;

    /*This second function allows to select as root special
    folders like printers, fonts, "send to", etc
    Using this and the flag BIF_BROWSEINCLUDEFILES
    You can present a dialog to select printers, fonts, etc
    //See SHGetSpecialFolderLocation for CSIDLspecialFolder possible values
    bool CFolderDialog::SetRootFolder(int CSIDLspecialFolder)
    HRESULT hr;

    hr = SHGetSpecialFolderLocation(NULL,CSIDLspecialFolder,&pidl);

    if (FAILED(hr))
    m_bi.pidlRoot = NULL;
    return false;// Handle error.

    m_bi.pidlRoot = pidl;
    return true;

    The only feature that I think is missing now is the
    "New Folder" Button.


  • Just what I needed

    Posted by Legacy on 12/12/1999 08:00am

    Originally posted by: Glenn

    Hello Ken,

    I was looking for this very thing just a few days ago.
    I came across your page after searching deja.com.

    I appreciate this contribution!

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date