Click here to Skip to main content
15,889,096 members
Articles / Programming Languages / C++
Tip/Trick

FolderBrowser

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 May 2012CPOL1 min read 17.5K   402   7   4
Microsoft don't have a nice interface that used to select a specify folder.

Microsoft don't have a nice interface that used to select a specify folder. First, I search some code on the internet, but they don't work perfectly. So, I changed these code, and they become my FolderBrowser.

The class CFolderBrowser only has a static method, called SelectFolder. So you can call it without creating an object of FolderBrowser. In fact, we can just use a c style function to implent it. The core code of SelectFolder, call windows's Shell API.

  • SHGetSpecialFolderLocation(): It can get the Desktop's path.
  • SHBrowseForFolder(): It can show the window to select folder.
  • SHGetPathFromIDList(): It can get the selected folder as a string.

SHBrowseForFolder need the struct BROWSEINFO 's pointer as its parameter. All the input info can be found in BROWSEINFO. The lpfn member can accept a callback function. When the user open the selecting window or select any folder, lpfn will be called. So, we can use the callback function to set the initial path, and change the title when user select any folder.

The API SetCurrentDirectory(), can save the current folder. So, when you select folder next time, GetCurrentDirectory() can get the saved folder. You can use the current folder as the initial path, or not, as you like.

At last, the example code:

C++
CString strPath;
::GetCurrentDirectory(MAX_PATH, strPath.GetBuffer(MAX_PATH));
strPath.ReleaseBuffer();
if (CFolderBrowser::SelectFolder(this->m_hWnd, strPath))
 m_edit.SetWindowText(strPath);        // m_edit is an object of CEdit

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Alien51230-Jul-12 5:17
Alien51230-Jul-12 5:17 
GeneralMy vote of 5 Pin
Volynsky Alex19-May-12 8:18
professionalVolynsky Alex19-May-12 8:18 
QuestionDownloading does not work Pin
anlarke15-May-12 2:50
anlarke15-May-12 2:50 
AnswerRe: Downloading does not work Pin
liaoy74715-May-12 5:06
liaoy74715-May-12 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.