MFC弹出浏览对话框
2021-09-07
32
0
CString strTemp;
CString str;
BROWSEINFO bi;
char Buffer[MAX_PATH];
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = Buffer;
bi.lpszTitle = "数据存储路径";
bi.ulFlags = 0;
bi.lpfn = NULL;
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
strTemp.Format("%s\\",Buffer);
CTime t = CTime::GetCurrentTime();
str.Format("%d-%d-%d %d_%d_%d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());
str = strTemp + str;
CreateDirectory(str,NULL);
SetCurrentDirectory(str);
}
释放内存:
CString BrowseFolder(HWND hWnd,LPCTSTR lpTitle)
{
//调用SHBrowseForFolder取得目录(文件夹)名称。hWnd:父窗口句柄;lpTitle:窗口标题
char szPath[MAX_PATH]={0};
BROWSEINFO m_bi;
m_bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
m_bi.hwndOwner = hWnd;
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = lpTitle;
m_bi.lpfn = NULL;
m_bi.lParam = NULL;
m_bi.pszDisplayName = szPath;
LPITEMIDLIST pidl = SHBrowseForFolder(&m_bi);
if(pidl)
{
if(!SHGetPathFromIDList(pidl,szPath))szPath[0]=0;
IMalloc * pMalloc = NULL;
if(SUCCEEDED(SHGetMalloc(&pMalloc))) //取得IMalloc分配器接口
{
pMalloc->Free(pidl); //释放内存
pMalloc->Release(); //释放接口
}
}
return szPath;
}