Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CYABFFW: Yet Another BrowseForFolder Wrapper

0.00/5 (No votes)
12 Jun 2007 1  
Yet another wrapper for the ::BrowseForFolder function

CYABFFW in action

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:

CYABFFW dlg();
if (IDOK == dlg.DoModal())
{
    CString s = dlg.GetPath();
    // Do something with `s' ...

SHBrowseForFolder() allows quite a bit of customization of the dialog's appearance and behavior. You do this by filling out a BROWSEINFO struct 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:

CYABFFW dlg(_T("Please select a directory"), // Hint to user

            BIF_USE_NEWUI,                   // Flags for the dlg

            this,                            // Parent window

            CSIDL_DRIVES);                   // Root of search

if (IDOK == dlg.DoModal())
{
    CString s = dlg.GetPath();
    // Do something with `s' ...

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.

A list of licenses authors might use can be found here