MFC Wrapper for SHBrowseForFolder

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

Description

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.

Revisions

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



Comments

  • Nike Wind Max+instagram, at one's desire you take the color to be in on your feet!

    Posted by madytreathy on 04/21/2013 08:10pm

    Remember in 2008, if not earlier, when Nike launched on of the independent shoe color projects, the watchword "Bound Your Colours", "Nike PhotoiD" layout, [url=http://northernroofing.co.uk/roofins.cfm]nike free run uk[/url] response has not been as avid as expected. About, 2008 Canon IXUS 80 IS Digital greetings card arcade but contrariwise 8 million pixels, Nokia, the motorized phone market is the one regulation, NikeiD was support to color in the photos as a essence for sneakers custom color, although interesting, but does provoke some. Instagram which communicate this passion fun and elemental, Nike PHOTOiD homeopathic upgrade customization services, recently [url=http://markwarren.org.uk/goodbuy.cfm]nike free uk[/url] released a unique plan. That such iD can you utensil pictures as instagram account shoe color, little while offer Nike Breeze Max shoes and Nike Refresh Max 1, Nike Feeling Max 90 953 options. Interested in children's shoes, you [url=http://fossilsdirect.co.uk/glossarey.cfm]nike huarache[/url] can always vanish into thin air's official website photoid.Nike.com, in addition to flick through other people's ingenious sweat, or you can hear to upload your own instagram photo, erect your own Nike Hauteur Max.

    Reply
  • Thanks, works great.

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

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

    Reply
  • exactly what i needed, but...

    Posted by mensch on 09/14/2005 11:09pm

    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

    Reply
  • Nifty!

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

    Originally posted by: Mohamed Mustafa

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

    Reply
  • Memory leak somewhere :((

    Posted by Legacy on 04/03/2003 12: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?

    Thanks

    Reply
  • Good Class

    Posted by Legacy on 09/25/2001 12: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?

    Reply
  • THX a lot

    Posted by Legacy on 09/17/2001 12: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.

    Reply
  • Browse for folder

    Posted by Legacy on 09/08/2001 12: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
    Johnny

    Reply
  • please help !!

    Posted by Legacy on 04/20/2001 12: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!

    Reply
  • Another feature -> Function to set the root path

    Posted by Legacy on 02/22/2000 12: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)
    {
    LPITEMIDLIST pidl;
    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,
    NULL,
    olePath,
    &chEaten,
    &pidl,
    &dwAttributes);
    if (FAILED(hr))
    {
    m_bi.pidlRoot = NULL;
    return false;// Handle error.
    }

    m_bi.pidlRoot = pidl;
    //release the desktop folder object
    pDesktopFolder->Release();
    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)
    {
    LPITEMIDLIST pidl = NULL;
    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.

    Bye
    Jose

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds