MFC 文件夹选择对话框
CString setSavePath() { CString strPath = _T(""); HRESULT hr; LPITEMIDLIST pItemList; BROWSEINFO browseinfo; TCHAR path[MAX_PATH]; hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (FAILED(hr)) { TRACE("CoInitEx failed: %x\n", hr); return strPath; } IMalloc *pMalloc = NULL; hr = SHGetMalloc(&pMalloc); if (FAILED(hr)) { TRACE("Can't retrieve system's IMalloc interface: %x\n", hr); return strPath; } ENSURE(pMalloc); memset(&browseinfo, 0, sizeof(BROWSEINFO)); CString strCaption; browseinfo.hwndOwner = GetSafeHwnd(); browseinfo.pszDisplayName = path; browseinfo.ulFlags = BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS; strCaption.LoadString(IDS_SAVEPATH_CONFIG); browseinfo.lpszTitle = strCaption; pItemList = ::SHBrowseForFolder(&browseinfo); if (pItemList) { // Behold the elegance of CStrBuf! ::SHGetPathFromIDList(pItemList, CStrBuf(strPath, MAX_PATH)); pMalloc->Free(pItemList); UpdateData(FALSE); } pMalloc->Release(); pMalloc = NULL; ::CoUninitialize(); return strPath; }