首页 > 代码库 > Windows--MFC选择文件对话框和选择文件夹对话框

Windows--MFC选择文件对话框和选择文件夹对话框

选择单个文件对话框:

1 CString strFile = _T("");
2 
3 CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.jpg)|*.jpg|All Files (*.*)|*.*||"), NULL);
4 
5 if (dlgFile.DoModal())
6 {
7      strFile = dlgFile.GetPathName();
8 }

选择多个文件对话框

 1 void CBugReportDlg::SelectFiles( vector<CString>& all_sel_files )
 2 {
 3     CFileDialog dlg(TRUE, _T("*.jpg"), NULL, OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, _T("Image Files(*.jpg)|*.jpg|"), NULL); 
 4     dlg.m_ofn.lpstrTitle = _T("选择图片"); 
 5     CString filename; 
 6     if(dlg.DoModal() == IDOK) 
 7     { 
 8         POSITION fileNamesPosition = dlg.GetStartPosition(); 
 9         while(fileNamesPosition != NULL) 
10         { 
11             filename = dlg.GetNextPathName(fileNamesPosition); 
12             all_sel_files.push_back(filename);
13         }   
14     } 
15 
16 }

选择文件夹

 1 CString CBugReportDlg::SelFilePath()
 2 {
 3     TCHAR           szFolderPath[MAX_PATH] = {0};  
 4     CString         strFolderPath = TEXT("");  
 5 
 6     BROWSEINFO      sInfo;  
 7     ::ZeroMemory(&sInfo, sizeof(BROWSEINFO));  
 8     sInfo.pidlRoot   = 0;  
 9     sInfo.lpszTitle   = _T("请选择处理结果存储路径");  
10     sInfo.ulFlags   = BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_DONTGOBELOWDOMAIN;  
11     sInfo.lpfn     = NULL;  
12 
13     // 显示文件夹选择对话框  
14     LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);   
15     if (lpidlBrowse != NULL)  
16     {  
17         // 取得文件夹名  
18         if (::SHGetPathFromIDList(lpidlBrowse,szFolderPath))    
19         {  
20             strFolderPath = szFolderPath;  
21         }  
22     }  
23     if(lpidlBrowse != NULL)  
24     {  
25         ::CoTaskMemFree(lpidlBrowse);  
26     }  
27 
28     return strFolderPath;  
29 }

 

Windows--MFC选择文件对话框和选择文件夹对话框