The Windows
Directory Picker
Folder Browser
Site Map Feedback

Download:

Up DTree Local Picker Recycle ResFile Temp

Access MFC Resources as if they were MFC CFiles

MFC does not provide access to the directory-picking dialog as it does the other Common Dialogs.
You can use the ShBrowseForFolder function; the following two functions demonstrate the simplest way to use it:
void CMyDlg::OnButton1() {
  char Dest[MAX_PATH];
  PickDir("Hello", Dest);
}

bool CMyDlg::PickDir(const char* Prompt, char* Dest) {
// Dest is assumed to be _MAX_PATH characters in length
  BROWSEINFO bi;
  ITEMIDLIST* pItemIDList;
  char Folder[_MAX_PATH];
  memset(&bi, 0, sizeof(bi));
  bi.hwndOwner=m_hWnd;
  bi.pszDisplayName=Folder;
  bi.lpszTitle=Prompt;
  if((pItemIDList=SHBrowseForFolder(&bi))==NULL) return false;
  SHGetPathFromIDList(pItemIDList, Dest);
  return true;
}
There is also Callback functionality available - but most people find that confusing, so it is encapsulated it in a class (CBrowseForFolder) that should help and provided a couple of useful example classes.

CBrowseForDirectory demonstrates the usage of CBrowseForFolder and gets the user to pick a valid Directory (ie not the "Recycle Bin" or "My Computer" etc).
CBrowseForFile also demonstrates the usage of CBrowseForFolder and gets the user to tell us where a particular file is by only enabling the [OK] button when a folder containing a particular file is selected.
Look at the CBrowseForFolder class to see the virtual functions you can override to receive information,
and the functions you can use to control the Dialog Box (the ones that use SendMessage).

This file also contains a horrible little class that encapsulates the revolting code necessary to find the PIDL of a path...
You shouldn't need CItemIDList because there is already a SelectItem function that takes a path, but it is included to show how to do it.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1290) Last modified: Fri, 15 May 2009 15:27:43 +0100