select multiple files in CFileDialog
Asked Answered
mfc
C

5

6

In VC++ 6.0, MFC I want to select a multiple files

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

The code sample above allows only a single file being selected at a time, but I want to select multiple text files, for example by holding down control key (ctrl+select the multiple files). How can I achieve this?

Crumbly answered 10/7, 2009 at 5:52 Comment(0)
A
11

So in the constructor for CFileDialog you can set the dwFlags parameter to have 'OFN_ALLOWMULTISELECT'. Thats the easy part, to actually get the multiple file names back you have to modify the m_ofn.lpstrFile member in the CFileDialog to point to a buffer that you have allocated. Have a look here:

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

Here is an example use of it, hope the comments suffice:

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; 
}
Autotruck answered 10/7, 2009 at 6:0 Comment(3)
still i am confusion , i am not getting clearly, please will u give me sample of code or any example,or please modify my code given above thank youCrumbly
don't you need to call str.ReleaseBuffer() after DoModal()?Fever
@Magnus: I agree! Should also do FileDlg.GetOFN().nMaxFiles = nMaxFiles before DoModal.Anachronous
P
7

An example:

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
}
Puling answered 15/7, 2014 at 0:9 Comment(0)
D
1

You should pass the OFN_ALLOWMULTISELECT flag in OpenFileName structure to allow the multi selection.

Digenesis answered 10/7, 2009 at 5:59 Comment(1)
please can you me a example so that i will get more clear ,thank youCrumbly
K
0

Insert this line:

opendialog.m_ofn.Flags |= OFN_ALLOWMULTISELECT;

Or set the flag in the CFileDialog constructor as DeusAduro did.

Ketosis answered 1/3, 2012 at 1:22 Comment(0)
M
0

None of the supplied answers are complete. Some are wrong in terms of parameters.

Here's an example of multi-file selection and open:

// 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;
Miterwort answered 21/3 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.