MFC读取单个文件/文件名目录/文件目录下所有文件

1.读取单个文件路径

 1 Invalidate();
 2 
 3     CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_READONLY,
 4         TEXT("Supported Types (*.jpg;*.png;*.gif;*.bmp;...)|*.jpg;*.png;*.gif;*.bmp|Tiff(*.tiff;*.tif)|*.tiff;*.tif|All Files(*.*)|*.*||"), NULL);
 5     dlg.m_ofn.nFilterIndex = 1;
 6     dlg.m_ofn.hwndOwner = m_hWnd;
 7     dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
 8     dlg.m_ofn.lpstrTitle = TEXT("Opening Image...\0");
 9     dlg.m_ofn.nMaxFile = MAX_PATH;
10     if (dlg.DoModal() == IDOK)
11     {
12         m_path = dlg.GetPathName();
13         m_isOpen = TRUE;
14         UpdateData(FALSE);
15     }
16     else
17         return;
18     //左边图片控件显示图片
19     char* s_path;
20     USES_CONVERSION;
21     s_path = T2A(m_path);

 

2.读取多个文件的文件名

 1    vector<CString>fileDict;
     CFileDialog dlg(TRUE, _T("*"), NULL, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST, _T("所有文件 file (*.*)|*.*||"), NULL); 2 DWORD MAXFILE = 500 * MAX_PATH;// 最多打开500个文件 3 dlg.m_ofn.nMaxFile = MAXFILE; 4 TCHAR* buf = new TCHAR[MAXFILE]; 5 memset(buf, 0, sizeof(TCHAR) * MAXFILE); 6 dlg.m_ofn.lpstrFile = buf; 7 8 int iReturn = dlg.DoModal(); 9 if (iReturn == IDCANCEL) 10 { 11 return; 12 } 13 POSITION pos = dlg.GetStartPosition(); 14 while (pos != NULL) 15 { 16 fileDict.push_back(dlg.GetNextPathName(pos)); 17 } 18 19 delete[] buf;
    }

 

 

3.选择文件夹路径

 1 CString m_saveFilePath;
 2     TCHAR szPath[_MAX_PATH];
 3     BROWSEINFO bi;
 4     bi.hwndOwner = GetSafeHwnd();
 5     bi.pidlRoot = NULL;
 6     bi.lpszTitle = _T("请选择图片路径");
 7     bi.pszDisplayName = szPath;
 8     bi.ulFlags = BIF_RETURNONLYFSDIRS;
 9     bi.lpfn = NULL;
10     bi.lParam = NULL;
11 
12     LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);
13     if (pItemIDList)
14     {
15         if (SHGetPathFromIDList(pItemIDList, szPath))
16         {
17             m_saveFilePath = szPath;
18             m_saveFilePath = m_saveFilePath + _T("\\");
19         }
20 
21         //use IMalloc interface for avoiding memory leak  
22         IMalloc* pMalloc;
23         if (SHGetMalloc(&pMalloc) != NOERROR)
24         {
25             TRACE(_T("Can't get the IMalloc interface\n"));
26         }
27 
28         pMalloc->Free(pItemIDList);
29         if (pMalloc)
30             pMalloc->Release();
31         UpdateData(FALSE);
32     }

 

posted @ 2019-09-27 17:55  荼离伤花  阅读(2679)  评论(0编辑  收藏  举报