使用CFileDialog选择多个文件(VC)

旧时模样 提交于 2019-12-05 19:22:19

李国帅 于2009-07-08 17:21

选择一个文件

//CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("AVI文件(*.avi)|*.AVI|mp4文件(*.mp4)|*.MP4|jpg文件(*.jpg)|*.jpg||"));
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("媒体文件(*.avi;*.mp4)|*.AVI;*.MP4||"));
if (IDOK == dlg.DoModal())
{
    CString aviName = dlg.GetPathName();
    CString extname = dlg.GetFileExt(); //返回选定文件的扩展文件名
    extname.MakeLower();
}

选择多个文件

CString fileNameArray[numberOfFileNames];// Create array for file names.
void CCombinDlg::OnBnClickedBtnAdd()
{
    // Create dialog to open multiple files.
    CFileDialog Filedlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_EXPLORER, _T("AVI文件(*.avi)|*.AVI||"));
    // Create buffer for file names.
    const DWORD numberOfFileNames = 32;//最多允许32个文件
    const DWORD fileNameMaxLength = MAX_PATH + 1;
    const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;
    TCHAR* filenamesBuffer = new TCHAR[bufferSize];
    // Initialize beginning and end of buffer.
    filenamesBuffer[0] = NULL;//必须的
    filenamesBuffer[bufferSize - 1] = NULL;

    // Attach buffer to OPENFILENAME member.
    Filedlg.m_ofn.lpstrFile = filenamesBuffer;
    Filedlg.m_ofn.nMaxFile = bufferSize;

    int iCtr = 0;
    if (IDOK == Filedlg.DoModal())
    {
        CString aviName;
        POSITION pos = Filedlg.GetStartPosition();
        while (pos != NULL)
        {
            aviName = Filedlg.GetNextPathName(pos);//返回选定文件文件名// Retrieve file name(s).
            fileNameArray[iCtr] = aviName;
            iCtr++;
        }
    }

    delete[] filenamesBuffer;//Release file names buffer.
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!