在CFileDialog中选择多个文件

问题描述 投票:0回答:5

在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;
    ...
}

上面的代码示例一次只能选择一个文件,但我想选择多个文本文件,例如通过按住 Ctrl 键(ctrl+选择多个文件)。我怎样才能实现这个目标?

mfc
5个回答
11
投票

因此,在 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; 
}

7
投票

一个例子:

CString sFilter = _T("XXX Files (*.xxx)|*.xxx|All Files (*.*)|*.*||");


CFileDialog my_file_dialog(TRUE, _T("xxx"),NULL,
                           OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT,
                           sFilter, this);

if ( my_file_dialog.DoModal()!=IDOK )
    return;

POSITION pos ( my_file_dialog.GetStartPosition() );
while( pos )
{
    CString filename= my_file_dialog.GetNextPathName(pos);

    //do something with the filename variable
}

1
投票

您应该在 OpenFileName 结构中传递 OFN_ALLOWMULTISELECT 标志以允许多重选择。


0
投票

插入这一行:

opendialog.m_ofn.Flags |= OFN_ALLOWMULTISELECT;

或者像 DeusAduro 一样在 CFileDialog 构造函数中设置标志。


0
投票

提供的答案都不完整。有些参数是错误的。

以下是多文件选择并打开的示例:

// an approximate arbitrary file limit of 200
uint32_t maxOpenFiles = 200;    

// A buffer to hold 200 file paths (using the old 260 char path limit). Going to assume its allocated.
TCHAR* lpszFiles = new TCHAR[maxOpenFiles * MAX_PATH]; // MAX_PATH is 260 here
lpszFiles[0] = 0; // don't forget this!

CFileDialog dlgOpen(TRUE, L"*", NULL, OFN_EXPLORER | OFN_ALLOWMULTISELECT, L"All Files (*.*)|*.*||");

dlgOpen.m_ofn.lpstrFile = lpszFiles;
dlgOpen.m_ofn.nMaxFile = maxOpenFiles * MAX_PATH;   // not the file count, but the size of the buffer in characters.

if (dlgOpen.DoModal() == IDOK)
{
    CString sFilePath;

    POSITION pos = dlgOpen.GetStartPosition();

    while (pos)
    {
        // fetch each file path
        sFilePath = dlgOpen.GetNextPathName(pos);

        // do something with the file path
        // OpenMe(sFilePath);
    }
}

// free the path buffer (assuming it was allocated)
delete[] lpszFiles;
© www.soinside.com 2019 - 2024. All rights reserved.