MFC对话框选择多个文件及选择文件夹

  • 选择多个文件(这里选择多张图片)
void SelctFiles()
{
    CFileDialog dlg(TRUE, _T("*.jpg"), NULL, OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, _T("Image Files(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp|All Files (*.*)|*.*||"), NULL); 
dlg.m_ofn.lpstrTitle = _T("选择图片"); 
CString filename; 
vector vecPicPaths;
if(dlg.DoModal() == IDOK) 
{ 
    POSITION fileNamesPosition = dlg.GetStartPosition(); 
    while(fileNamesPosition != NULL) 
    { 
        filename = dlg.GetNextPathName(fileNamesPosition); 
        vecPicPaths.push_back(filename);
    }   
} 

int count = vecPicPaths.size();
if (count == 0)
    MessageBox(_T("没有选择文件"), NUL, MB_OK|MB_ICONQUESTION);
  • 选择文件夹

    static CString ShowDialog()  
    {  
        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;  

    }  

你可能感兴趣的:(Windows)