//代码如下,sFolder保存了用户选择的文件夹路径。
void CXXXX::OnOpenfolder()
{
CString sFolder;
LPMALLOC pMalloc;
//Gets the Shell's default allocator
if(::SHGetMalloc(&pMalloc) == NOERROR)
{
BROWSEINFO bi;
char pszBuffer[MAX_PATH];
LPITEMIDLIST pidl;
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = _T("Select a directory...");
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
//This next call issues the dialog box.
if((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if(::SHGetPathFromIDList(pidl, pszBuffer))
{
//At this point pszBuffer contains the selected path
sFolder = pszBuffer;
}
//Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
//Release the shell's allocator.
pMalloc->Release();
}
}