Introduction

YABFFW (Yet Another BrowseForFolder Wrapper) is a CWnd subclass wrapping the Shell API SHBrowseForFolder() in an MFC-friendly way. There are certainly others out there, but the approach taken here (in my opinion) integrates more cleanly into MFC apps. I also believe that it makes it easier to customize the appearance and behavior of the dialog box.

Background

Sooner or later, we all need to display a dialog allowing the user to select a directory. A search in MSDN turns up the function ::SHBrowseForFolder() (<shlobj.h>). The next step is usually to either write or steal a C++ class that wraps up the C-style function call in a more MFC-friendly way.

At least, that's what happened to me. In my case, I needed to customize the dialog a bit, too. Specifically, I wanted to add a little checkbox to it labeled "Recurse" and retrieve that checkbox's value after the user dismissed the dialog. So, I started surfing the web looking for examples. I found several -- C++ wrappers, as well as examples of customizing the "Browse for Folder" dialog -- and happily started copying them. However, as I implemented the code I began to see a cleaner and more extensible way to accomplish my task. CYABFFW is the product of that process.

Using the code

What makes CYABFFW different is that it is a CWnd that subclasses the "Browse for Folder" dialog right after creation. This gives the class access to things like Message Maps, CObject diagnostics, DDX/DDV and so on. It also gets us out of calling ::SetWindowLong, writing a WndProc and suchlike. Of course, it commits you to MFC. If you're not using MFC, CYABFFW won't be of much use to you. The source code is shown below, although you can of course download it along with a sample project.

How do I use it?

In the simplest case, CYABFFW looks and acts just like most MFC dialog classes. You instantiate it on the stack, call DoModal() and then check the return value to see what the user did. Once the user hits "OK," you can retrieve the item they selected by calling either GetPath() or GetItemIdList(), depending on whether you want the selection as a path or an ITEMIDLIST. For example:

SHBrowseForFolder() allows quite a bit of customization of the dialog's appearance and behavior. You do this by filling out a BROWSEINFOstruct that is passed to the function. I've tried to expose all of the functionality provided by SHBrowseForFolder() in a way more familiar to C++ programmers. The first point of extension is the CYABFFW constructor. There are several constructors, each taking an array of parameters that control the resulting dialog. For a full list, refer to the source code. Here's an example, however:

If you want to customize the dialog beyond what you can do with arguments to the constructor, you'll need to subclass CYABFFW. CYABFFW defines three virtual functions that can be overridden: OnInitBFFDialog(), OnBFFSelChanged() and OnBFFValidateFailed(). These correspond to the custom messages that SHBrowseForFolder() sends to its optional callback function BFFM_INITIALIZED, BFFM_SELCHANGED, BFFM_VALIDATEFAILED. Of course, you can also Message Map entries to your subclass and process any Windows messages you're interested in. The demo project includes an example of doing this to add my "Recurse" checkbox.

Setting the initial selection

One question that came up after I posted the first version of this article was how to set the initial selection for the dialog. Microsoft KB article 179378 recommends handling the BFFM_INITIALIZED notification and sending yourself the BFFM_SETSELECTION message. I decided to build out the support for this: CYABFFW constructors now take an optional default selection and CYABFFW::OnInitBFFDialog() will handle setting the selection appropriately. Therefore, if you subclass CYABFFW and override OnInitBFFDialog, make sure you call the base class implementation, too.

History

Date posted: May 7, 2005.

Date first update: May 10, 2005.

Date second update: June 12, 2007.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

When i compile this demo of this project i get the error
error C2039: 'SHGetFolderLocation' : is not a member of '`global namespace''
error C2065: 'SHGetFolderLocation' : undeclared identifier

Pls pls help me, i have included both shlodj.h and shell32.lib but it still not working. pls do reply becoz i have my own work regarding Shells stucked because of this error, and can't find enough on google.

I'd like to help, but it's a little tough without more information. I just built it, and I see that I'm picking up SHGetFolderLocation from ShlObj.h in the Platform SDK (i.e. from C:\Program Files\Microsoft SDK\include\shlobj.h). Can you tell me more about your build environment? Do you have the platform SDK installed? I assume you're using DevStudio 6, yes? If you grep through your include directories, can you find SHGetFolderLocation? It's only there (in my file) if _WIN32_IE >= 0x0500... could that have been turned off somehow in your environment?

Yes, I would care to explain. I'm the same person. I'd always meant to submit the article in both places, but got distracted by other projects a few years ago. I was cleaning out my old projects the other weekend, and submitted it here at the code project.

Plagiarism is a serious accusation. You might try just asking next time.

I find even wirdier that a person takes some time to ask another one
publicly, if he/she has the rights or not to something else that he/she
does not have rights over in the first place...

Worst, if you get a reply to your questions and you did use that work
without the author permission you can get into deeper troubles that if
you didn't know in the first place...

Ownership rights (every one of them are evil, but still necessary until
we get over Capitalism and find a better system), Native Americans did
get it right before they got absorbed into the global village...

Sorry to hear that. I just downloaded the sample program & compiled succesfully under VC6, so there's *some*thing different about our systems. My build's picking up the symbols you list from the Platform SDK; do you have that installed?

If not, and that's the problem, let me know so I can update the article to note the fact that the PSDK is required.