SHBrowseForFolder shell API function shows a dialog which allows users to select a folder. As can be seen in MSDN documentation or other examples, it requires pretty much additional code around, so you would prefer to write your own wrapper in order to reuse code along applications. But luckily, MFC library has that wrapper already done.
Browse for folder using MFC
All you have to do is the following:
- derive your application class from CWinAppEx;
- whenever you want to show a “browse for folder” dialog
- call CWinAppEx::GetShellManager to get a pointer to a global CShellManager object then
- call CShellManager::BrowseForFolder.
A simple example of using CShellManager::BrowseForFolder
// note: the application class is derived from CWinAppEx
CShellManager* pShellManager = theApp.GetShellManager();
// do something cool!
- CWinAppEx as well as CShellManager classes were introduced with MFC Feature Pack. That means you need Visual Studio 2008 or newer;
- CShellManager::BrowseForFolder has additional default parameters which can be used for more customization;
- a little bit more complete example can be found in the demo application.
MFC Browse For Folder demo application
Download: MFC BrowseForFolder Demo.zip (1665)