分类: C/C++
2008-08-31 18:40:31
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.
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.
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.
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.
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.