Click here to Skip to main content
11,934,425 members (31,167 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Opening multipe documents of several types at once

, 15 May 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
This class provides an open file dialog that shows multiple file types at once, and allows selecting multiple files at once.

Open dialog showing multiple files types and two selected files


If you have a MDI application, it is not uncommon to also have several document classes. The MFC's default implementation of the open file dialog has two shortcomings that your users may find annoying: it only shows one file type at a time, and does not allow to open multiple files at once. The class explained in this article solves these issues.

Using the code

  • Add the four files included in the source code to your project.
  • Add a new string to your string table called IDS_ALLDOCSFILTER, this will be the string shown as the filter name.

    the string table of your app

  • Include the extended doc manager header in your CWinApp.cpp file:
    #include "DocManager2.h"
  • Create the doc manager in the first line of your InitInstance CWinApp. Notice that m_pDocManager is a member of the base class.
    BOOL CMyApp::InitInstance()
        m_pDocManager= new CDocManager2();
        ... the rest of your code ...
  • That is it! That was easy! Compile and try to open a file to test the new functionality.

Inside the class

If you take a peek under the CWinApp hood, you will find that it delegates file related functions to an instance of the undocumented class CDocManager (Roger Allen has a nice explanation about CDocManager here). This class is actually responsible for showing the open file dialog and for opening the document, so it is the ideal candidate for our task at hand. So let's get into work.

Showing all file types at once

The first step is to override the CDocManager::DoPromptFileName function. This function was not really made to be overridden, so in order to make a slight change, you have to copy the base class code and tweak it a little. The following code adds a new filter to the filter list:

BOOL CDocManager2::DoPromptFileName(CString& fileName, 
    UINT nIDSTitle, DWORD lFlags,
    BOOL bOpenFileDialog, CDocTemplate* pTemplate)
    ... original code ...
    if (pTemplate != NULL)
                dlgFile.m_ofn, pTemplate, &strDefault);
        // append all docs filter
        CString strFilters;
        POSITION pos = m_templateList.GetHeadPosition();
        // for each doc template
        while (pos != NULL)
            CDocTemplate* pTemplate = 
            CString strFilterExt;
            // append its extension to the filter list
        // delete the trailing ;

        CString allDocsFilter;
        strFilter += allDocsFilter;
        strFilter += (TCHAR)'\0';   // next string please
        strFilter += strFilters;
        strFilter += (TCHAR)'\0';   // last string
    ... original code ...
    // select the all documents filter as the default filter
    INT_PTR nResult = dlgFile.DoModal();
    return nResult == IDOK;

Unfortunately, this code uses the internal MFC function _AfxAppendFilterSuffix. To make the code compile, the only solution I found was to copy the code verbatim from MFC source files. Let's hope it does not change a lot in future versions of MFC!

Opening multiple files at once

To open multiple files at once, we must override CDocManager::OnFileOpen. Apart from adding the obvious OFN_ALLOWMULTISELECT flag, we must add code to iterate the selected files list and open the documents one by one:

void CDocManager2::OnFileOpen()
    // prompt the user (with all document templates)
    CString newName;
    if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
        return; // open cancelled
    LPSTR szName=newName.GetBuffer();
    // replace all the embebbed nulls, CString can not handle them well
    for(int i=0;i<newName.GetAllocLength();i++)

    // get a vector of all names
    std::vector<CString> vectorFiles=split(newName,_T("*"));

    CString strPath=vectorFiles[0];
        for(std::vector<CString>::iterator i=vectorFiles.begin()+1;

Now, we have a class that behaves as we want. Happy coding!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ernesto Perales Soto
Web Developer
Mexico Mexico
@Work : Doing UML modelling for food
@Home : Still coding for fun!

You may also be interested in...

Comments and Discussions

Generalcompiles with Visual Studio 2010 on Windows Server 2008 64-bit but ... Pin
RedDK23-May-11 9:59
memberRedDK23-May-11 9:59 
GeneralRe: compiles with Visual Studio 2010 on Windows Server 2008 64-bit but ... Pin
Ernesto Perales Soto23-May-11 15:33
memberErnesto Perales Soto23-May-11 15:33 
GeneralRe: compiles with Visual Studio 2010 on Windows Server 2008 64-bit but ... Pin
RedDK24-May-11 6:45
memberRedDK24-May-11 6:45 
QuestionThe Code an not be compiled [modified] Pin
woodyzhang21-Jun-06 17:42
memberwoodyzhang21-Jun-06 17:42 
QuestionRe: The Code an not be compiled Pin
Ernesto Perales Soto22-Jun-06 4:45
memberErnesto Perales Soto22-Jun-06 4:45 
AnswerRe: The Code an not be compiled Pin
hu496382820-May-09 7:31
memberhu496382820-May-09 7:31 
GeneralMultiple Selection Pin
PJ Arends30-May-04 15:15
memberPJ Arends30-May-04 15:15 
GeneralGood catch.... Pin
Ernesto Perales Soto31-May-04 21:09
memberErnesto Perales Soto31-May-04 21:09 
GeneralVery handy ! Pin
WREY16-May-04 16:57
memberWREY16-May-04 16:57 
GeneralThanks for your words! Pin
Ernesto Perales Soto17-May-04 4:46
memberErnesto Perales Soto17-May-04 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 16 May 2004
Article Copyright 2004 by Ernesto Perales Soto
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid