MFC讀取單個文件/文件名目錄/文件目錄下全部文件

1.讀取單個文件路徑spa

 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.讀取多個文件的文件名code

 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.選擇文件夾路徑blog

 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     }
相關文章
相關標籤/搜索