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
1 2 3 4 5 6 7 8 |
CString strOutFolder; // note: the application class is derived from CWinAppEx CShellManager* pShellManager = theApp.GetShellManager(); ASSERT_VALID(pShellManager); if(pShellManager->BrowseForFolder(strOutFolder)) { // do something cool! } |
Notes
- 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)
Resources and related stuff
- MSDN: CShellManager::BrowseForFolder
- MSDN: CWinAppEx::GetShellManager
- MSDN: SHBrowseForFolder function
- Codexpert forum (RO): [WinAPI] Cum selectez un folder?
nice explaination