The Windows Shell API
SHBrowseForFolder function will let you do this. For example, if you have an MFC application project with an MFC dialog class, add #include <atlbase.h> to your stdafx.h, then add this function to your project:
static bool ChooseFolder(HWND hParent, const CString& title, CString& folder)
{
bool success = false;
BROWSEINFO bi;
::ZeroMemory(&bi, sizeof(bi));
LPTSTR pBuffer = folder.GetBuffer(MAX_PATH);
bi.hwndOwner = hParent;
bi.pszDisplayName = pBuffer;
bi.lpszTitle = title;
bi.pidlRoot = 0;
bi.ulFlags = BIF_RETURNONLYFSDIRS |
BIF_NEWDIALOGSTYLE;
LPITEMIDLIST pItem = ::SHBrowseForFolder(&bi);
if (pItem != NULL)
{
::SHGetPathFromIDList(pItem, pBuffer);
success = true;
CComPtr<IMalloc> pMalloc;
if (SUCCEEDED(::SHGetMalloc(&pMalloc)))
pMalloc->Free(pItem);
}
folder.ReleaseBuffer();
return success;
}
Then you can use this function from your dialog class like this:
CString folder;
if (ChooseFolder(m_hWnd, _T("Choose a folder:"), folder))
MessageBox(folder);
- Elton