选择单个文件对话框:
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.jpg)|*.jpg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
选择多个文件对话框
void CBugReportDlg::SelectFiles( vector<CString>& all_sel_files )
{
CFileDialog dlg(TRUE, _T("*.jpg"), NULL, OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, _T("Image Files(*.jpg)|*.jpg|"), NULL);
dlg.m_ofn.lpstrTitle = _T("选择图片");
CString filename;
if(dlg.DoModal() == IDOK)
{
POSITION fileNamesPosition = dlg.GetStartPosition();
while(fileNamesPosition != NULL)
{
filename = dlg.GetNextPathName(fileNamesPosition);
all_sel_files.push_back(filename);
}
}
}
选择文件夹
CString CBugReportDlg::SelFilePath()
{
TCHAR szFolderPath[MAX_PATH] = {0};
CString strFolderPath = TEXT("");
BROWSEINFO sInfo;
::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
sInfo.pidlRoot = 0;
sInfo.lpszTitle = _T("请选择处理结果存储路径");
sInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_DONTGOBELOWDOMAIN;
sInfo.lpfn = NULL;
// 显示文件夹选择对话框
LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
if (lpidlBrowse != NULL)
{
// 取得文件夹名
if (::SHGetPathFromIDList(lpidlBrowse,szFolderPath))
{
strFolderPath = szFolderPath;
}
}
if(lpidlBrowse != NULL)
{
::CoTaskMemFree(lpidlBrowse);
}
return strFolderPath;
}