mfc - 在 CFileDialog 中选择多个文件

标签 mfc

在 VC++ 6.0, MFC 中我想选择多个文件

CFileDialog opendialog(true); // opens the dialog for open;
opendialog.m_ofn.lpstrTitle="SELECT FILE"; //selects the file title;
opendialog.m_ofn.lpstrFilter="text files (*.txt)\0*.txt\0"; //selects the filter;

if(opendialog.DoModal()==IDOK) //checks wether ok or cancel button is pressed;
{
    srcfilename=opendialog.GetPathName(); //gets the path name;
    ...
}

上面的代码示例一次只允许选择一个文件,但我想选择多个文本文件,例如按住 control 键(ctrl+选择多个文件)。我怎样才能做到这一点?

最佳答案

因此,在 CFileDialog 的构造函数中,您可以将 dwFlags 参数设置为“OFN_ALLOWMULTISELECT”。这是最简单的部分,要实际获取多个文件名,您必须修改 CFileDialog 中的 m_ofn.lpstrFile 成员以指向您已分配的缓冲区。看看这里:

http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.80).aspx

这是一个使用它的示例,希望评论足够:

void CMainFrame::OnFileOpen()
{
    char strFilter[] = { "Rule Profile (*.txt)|*.txt*||" };

    CFileDialog FileDlg(TRUE, "txt", NULL, OFN_ALLOWMULTISELECT, strFilter);
    CString str;
    int nMaxFiles = 256;
    int nBufferSz = nMaxFiles*256 + 1;
    FileDlg.GetOFN().lpstrFile = str.GetBuffer(nBufferSz);
    if( FileDlg.DoModal() == IDOK )
    {
        // The resulting string should contain first the file path:
        int pos = str.Find(' ', 0);
        if ( pos == -1 );
            //error here
        CString FilePath = str.Left(pos);
        // Each file name is seperated by a space (old style dialog), by a NULL character (explorer dialog)
        while ( (pos = str.Find(' ', pos)) != -1 )
        {   // Do stuff with strings
        }
    }
    else
        return; 
}

关于mfc - 在 CFileDialog 中选择多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1107913/

相关文章:

c++ - MFC:CSting IntelliSense:没有重载函数的实例

c++ - Invalidate() 调试断言失败消息(MFC、VC++)

c++ - 读取和写入注册表项。 C++ MFC MBCS。

c++ - 如何在MFC 的CScrollView 类中制作不可见的滚动条?

c++ - win7 + vc7.1 + mfc = 计算器

c++ - 隐藏多个 CMFCEditBrowseCtrl 的文件浏览按钮

c++ - 将 CFileDialog 的默认 View 设置为大图标

mfc - CefInitialize 无法在 mfc dll 中工作

c++ - MFC:从 CImage 保存到数据库作为选定的文件类型

c++ - 什么是 CStringArray 或 CSortStringArray。那么如何将这个数组按顺序排列呢?