Introduction
As most of you probably know, the shell function SHBrowseForFolder
is easy to use, but there are a few things that have to be done every time that hardly ever change. One example is having to free the ITEMIDLIST
pointer returned by the shell after the folder/file path is queried with SHGetPathFromIDList
. My class takes care of that for you, and it automatically sets up default values for other parameters.
Here's an example of using CXSBrowseFolder
taken from the demo application:
CXSBrowseFolder foo;
foo.ModifyStyle(BIF_NEWDIALOGSTYLE, 0);
foo.SetTitle("This is the title text. Use CXSBrowseFolder::SetTitle() to set it:");
char path[MAX_PATH];
switch (foo.Show(GetSafeHwnd(), path)) {
case CXSBrowseFolder::RET_OK:
MessageBox(path, "You Selected", MB_ICONINFORMATION | MB_TOPMOST);
break;
case CXSBrowseFolder::RET_CANCEL:
MessageBox("Operation cancelled.", "Info", MB_ICONINFORMATION | MB_TOPMOST);
break;
case CXSBrowseFolder::RET_NOPATH:
MessageBox("The shell did not return a path for the selected item!",
"Uh Oh", MB_ICONSTOP | MB_TOPMOST);
break;
}
This class is very simple, but here is an overview of the methods:
DWORD CXSBrowseFolder::GetStyle()
Description
- Returns the current style of the dialog
Parameters
Return
- The current style of the dialog (refer to the
SHBrowseForFolder
docs for style information)
DWORD CXSBrowseFolder::ModifyStyle(DWORD add, DWORD remove = 0)
Description
- Adds and/or removes styles from the dialog (See shell docs for available styles)
Parameters
add
: style(s) to add remove
(optional): style(s) to remove
Return
- Current style (after modifications)
void CXSBrowseFolder::SetTitle(LPSTR title)
Description
- Sets the text displayed above the tree view in the dialog
Parameters
title
: The text to be displayed
Return
CXSBrowseFolder::retCode CXSBrowseFolder::Show(HWND parent, LPSTR pathBuffer)
Description
- Shows the folder/file browse dialog with the current settings
Parameters
parent
: HWND
of a parent for the dialog pathBuffer
: Buffer that will be filled with the path information of the selected file/folder
Return
CXSBrowseFolder::RET_CANCEL
: User clicked the dialog's cancel button CXSBrowseFolder::RET_NOPATH
: The shell did not return a valid path for the selection CXSBrowseFolder::RET_OK
: The OK button was pressed and pathBuffer
should contain a valid path
This class doesn't support customizing the dialog, but Microsoft doesn't recommend that with the new dialog style anyway because it's resizeable.
Well, that's about it for this article. I hope you find this simple class as useful and time saving as I have.