Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This class provides an open file dialog that shows multiple file types at once, and allows selecting multiple files at once. See Also y y Download source files - 2.78 Kb Download demo project - 31.2 Kb
Introduction
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.
Include the extended doc manager header in your CWinApp.cpp file: Collapse | Copy Code
#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. Collapse | Copy Code
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.
you have to copy the base class code and tweak it a little. The following code adds a new filter to the filter list: Collapse | Copy Code
BOOL CDocManager2::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate) { ... original code ... if (pTemplate != NULL) { ASSERT_VALID(pTemplate); _AfxAppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault); } else { // append all docs filter CString strFilters; POSITION pos = m_templateList.GetHeadPosition(); // for each doc template while (pos != NULL) { CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos); CString strFilterExt; // append its extension to the filter list pTemplate->GetDocString(strFilterExt,CDocTemplate::filterExt); if(!strFilterExt.IsEmpty()) strFilters+=_T('*')+strFilterExt+_T(';'); } // delete the trailing ;
strFilters.Delete(strFilters.GetLength()-1,1); CString allDocsFilter; VERIFY(allDocsFilter.LoadString(IDS_ALLDOCSFILTER)); strFilter += allDocsFilter; strFilter += (TCHAR)'\0'; strFilter += strFilters; // next string please
strFilter += (TCHAR)'\0';
// last string
dlgFile.m_ofn.nMaxCustFilter++; ... original code ... // select the all documents filter as the default filter dlgFile.m_ofn.nFilterIndex=0; INT_PTR nResult = dlgFile.DoModal(); fileName.ReleaseBuffer(); return nResult == IDOK; }
CString newName; if (!DoPromptFileName(newName, AFX_IDS_OPENFILE, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, TRUE, NULL)) 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++) { if(_T('\0')==szName[i]) if(_T('\0')!=szName[i+1]) szName[i]=_T('*'); }
newName.ReleaseBuffer(); // get a vector of all names std::vector<CString> vectorFiles=split(newName,_T("*")); CString strPath=vectorFiles[0]; if(1==vectorFiles.size()) AfxGetApp()->OpenDocumentFile(strPath); else { for(std::vector<CString>::iterator i=vectorFiles.begin()+1; i!=vectorFiles.end();i++) { AfxGetApp()->OpenDocumentFile(strPath+_T('\\')+*i); } } }
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)