MFC 选择文件夹的对话框

时间:2021-05-14 21:37:22
CString m_FileSrc;         
TCHAR Buffer[MAX_PATH];
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
bi.ulFlags = BIF_RETURNONLYFSDIRS;    //要求返回文件系统的目录
bi.pszDisplayName = Buffer;            //此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("请选择文件夹");
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
 
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if(pIDList)
{
    SHGetPathFromIDList(pIDList, Buffer);
    //取得文件夹路径到Buffer里
    m_FileSrc = Buffer;//将文件夹路径保存在一个CString对象里
}
 
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) 
    return _T("");
//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();