Multiple File Selection Dialog

I wanted to open a number of files at the same time from a Visual C++/MFC application. However, a ClassWizard generated application (with file support) only allows for the opening of a single file at a time. Therefore, I derived my own custom class from CFileDialog called MFileDlg. Using this class, I can now specify multiple files to be opened from the Open File dialog.

Class Declaration:


class MFileDlg : public CFileDialog
{
DECLARE_DYNAMIC(MFileDlg)

public:
 MFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL , LPCTSTR lpszFileName = NULL,
  DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
  LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL) ;

 int DoModal();
 virtual ~MFileDlg();

protected:
 //{{AFX_MSG(MFileDlg)
 // NOTE - the ClassWizard will add and remove member functions here.
 //}}AFX_MSG

 TCHAR* m_pszFile;
 DECLARE_MESSAGE_MAP()
};

Class Implementation:


IMPLEMENT_DYNAMIC(MFileDlg, CFileDialog)

MFileDlg::MFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
 DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
 CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
 m_pszFile = new TCHAR[2048]; //set a 2K buffer to hold selected files
 m_pszFile[0] = '\0'; //initialize pointer;
}

BEGIN_MESSAGE_MAP(MFileDlg, CFileDialog)
//{{AFX_MSG_MAP(MFileDlg)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

MFileDlg::~MFileDlg()
{
 if (m_pszFile != NULL)
 delete [] m_pszFile; //cleanup
}

int MFileDlg::DoModal()
{
 ASSERT_VALID(this);
 ASSERT(m_ofn.Flags & OFN_ALLOWMULTISELECT); //make sure multiple file selection is on

 m_ofn.lpstrFile = m_pszFile; //initialize the OPENFILENAME structure
 m_ofn.nMaxFile = 2048;

 return CFileDialog::DoModal();
}

How to Use:

1. On your CwinApp-derived class, override the OnOpenFile() function.


void CDigilogApp::OnFileOpen() 
{
// TODO: Add your command handler code here
 MFileDlg dlgFile(TRUE);

 CString title, strFilter, strDefault;
 VERIFY(title.LoadString(AFX_IDS_OPENFILE));

 // do for all doc template
 POSITION pos = GetFirstDocTemplatePosition();
 BOOL bFirst = TRUE;
 while (pos != NULL)
 {
  CDocTemplate* pTemplate = GetNextDocTemplate(pos);
  AppendFilterSuffix (strFilter, dlgFile.m_ofn, pTemplate, 
  bFirst ? &strDefault : NULL);
  bFirst = FALSE;
 }

 // append the "*.*" all files filter
 CString allFilter;
 VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
 strFilter += allFilter;
 strFilter += (TCHAR)'\0';   // next string please
 strFilter += _T("*.*");
 strFilter += (TCHAR)'\0';   // last string
 dlgFile.m_ofn.nMaxCustFilter++;

 dlgFile.m_ofn.lpstrFilter = strFilter;
 dlgFile.m_ofn.lpstrTitle = title;
 dlgFile.m_ofn.hwndOwner = AfxGetMainWnd()->GetSafeHwnd();

 if (dlgFile.DoModal() == IDOK)
 {
  POSITION pos = dlgFile.GetStartPosition();
  while (pos != NULL)
  {
   CString strPath = dlgFile.GetNextPathName(pos);
   if (strPath.Find(":\\\\") == 1 && strPath.GetLength() > 4)
   {
    // this means we have an invalid path that looks like this:
    // C:\\cda.dgl
    // get rid of extra slash
    CString temp;
    temp = strPath.Left(3);
    temp += strPath.Mid(4);
    strPath = temp;
   }
   OpenDocumentFile(strPath);
  }
 }
} 
2. Add this declaration and function to your CwinApp- derived class .


static void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn,
CDocTemplate* pTemplate, CString* pstrDefaultExt); 

static void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn, 
CDocTemplate* pTemplate, CString* pstrDefaultExt)
{
 ASSERT_VALID(pTemplate);
 ASSERT_KINDOF(CDocTemplate, pTemplate);

 CString strFilterExt, strFilterName;
 if (pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt) &&
  !strFilterExt.IsEmpty() &&
  pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
  !strFilterName.IsEmpty())
 {
  // a file based document template - add to filter list
  ASSERT(strFilterExt[0] == '.');
  if (pstrDefaultExt != NULL)
  {
   // set the default extension
   *pstrDefaultExt = ((LPCTSTR)strFilterExt) + 1;  // skip the '.'
   ofn.lpstrDefExt = (LPTSTR)(LPCTSTR)(*pstrDefaultExt);
   ofn.nFilterIndex = ofn.nMaxCustFilter + 1;  // 1 based number
  }

  // add to filter
  filter += strFilterName;
  ASSERT(!filter.IsEmpty());  // must have a file type name
  filter += (TCHAR)'\0';  // next string please
  filter += (TCHAR)'*';
  filter += strFilterExt;
  filter += (TCHAR)'\0';  // next string please
  ofn.nMaxCustFilter++;
 }
}

History



Comments

  • 2013 ghd gold classic styler er ikke Ã¥ gÃ¥ glipp Valentinsdag gave i Norge

    Posted by carpinteyroybi on 06/14/2013 09:08am

    [url=http://ghdrettetangtilbud.qsite.dk/]ghd rettetang[/url] GHD hår straighteners ikke bare rette håret og at det ser elegant og skinnende. Ved å bruke dem riktig, kan du krølle, vende eller bølge håret. Det samme produktet som du bruker for retting kan også brukes til andre innovative stiler, dette er på grunn av sin avrundede plater og håndtak, designet spesielt for å støtte din wrist.The offentlige snart akseptert merket, og med på kort tid, nådde den suksess . Nå mange kjendiser og profesjonelle stylister beundre produkter av GHD. [url=http://www.rettetangnorgenews.net/]CHI rettetang[/url] GHD hår straighteners er all-round stylers som forlater håret utrolig vakkert og beundringsverdig. Disse elementene er så portabel og har noen egenskaper som gjør dem svært behagelige å reise med eller bære rundt. Dessuten gjør rettetangen en god gave til en kvinne på hennes bursdag og blir virkelig verdsatt av alle som mottar den som gave. Overrask noen behagelig på hennes bursdag med GHD Precious Gift Set. [url=http://www.rettetangnorgenews.net/]ghd rettetang pris[/url] En InStyle magazine beste skjønnhet kjøp for flere år running.Hai er keramiske plater er belagt for å beskytte håret mot varme. Dette flatt jern blir varm nok til å rette håret (og hår forblir rett), men ikke for varmt å skade håret. Pluss det er klart til bruk på under 30 sekunder. Kanskje den beste flatt jern på markedet i dag, er det FHI Heat Runway en favoritt blant proffene fordi det skaper elegante, glatt hår med bare en eller to pasninger, ifølge stylist Jonathan Hanousek i 2009 utgaven av InStyle Magazine, som kalte den en Best Beauty Buy for 2009. Med en prislapp på mer enn $ 300, vil FHI Heat Runway (med Tourmaline plater, selvfølgelig) vare livet ut.

    Reply
  • During using beast beats is more safely a improved than others headphones

    Posted by motherdhmm on 06/04/2013 06:08am

    [url=http://www.headphonescheaponlineaustralia.com/dre-beats-studio-c-66_68_71.html]beats studio[/url] With this year dr Dre beats constantly and popular Monster headphones groups more and more, multifarious people like the reverberate of Monster headphones draw them. However, with the headphones into the trade in in late-model years, the theurgy unscathed humbug, dr Dre beats, they are cheaper than the real dr Dre beats, dr Dre beats high-class impersonation products and verifiable Horribleness Beats manner, in the process of the imbalance only professionals can label them, where, in contract for to avoid you buyMonster Beats [url=http://www.headphonescheaponlineaustralia.com/dre-beats-studio-c-66_68_71.html]beats studio[/url] Where is the subdue of spell sound headphones? Nightmarishness headphones is why uncountable of my friends loved, why the Monster is the be featured of the moment presentation headphones? Hype, publicity, or what? Hanker after to grasp that the Miscreation would like to recognize the monster headphones of it with me to take the next. The most noted Living abortion headphones trend is not vested with the lusus naturae recording operator, not a convenient easy to impel Barbarity Solo HD and people hear the handle can be linked to look like horridness of noodles. Speaking of the dragon of noodles would like to give birth to him a end of people really, living abortion noodles in a habits sold completed of this why? This is because the hypnotic of the allure of the impression, the wizardry ring of noodles series headphones, Monster Beats Justbeats Solitary HD Purple on-ear headphones, names such as real, headphone line, not in any degree intertwined friends on the side of to use headphones to untangle a burdensome problem. Is the complete instant, the headset intrinsic. Ashamed headphones can genuinely play along improvise a consequential effect. The howl reviews of this headset, I believe the-ear headphones is not a can and comparable to the miraculous be activated of noodles no matter the shape, or internal. The half a mo is the incline update of fast-paced society. I have the courage of one's convictions pretend that the moment the missing link is also in the supplemental products, trends and sound combination. This is the hypnotize of monster. [url=http://www.headphonescheaponlineaustralia.com/dre-beats-studio-c-66_68_71.html]beats studio[/url] Bogeyman¡¯s after 20 years of carrying elsewhere trade in decline. Age there are two hundred four thousand kinds of products and extensive clear several. The occultism impression of the gathering¡¯s products won numerous electrical production, the Lay out Alteration Endowment and the calibre award. Many acclaimed singers in the Synergistic States put to use professional wire of the occultism ring of their instruments, including myriad of the musicians made ??the Grammy Give (the Grammy Awards). The illustrious recording artists, engineers and recording studios say Monster cables to transactions their music.

    Reply
  • Easier Way to Select Multiple Files.

    Posted by Legacy on 09/26/2003 12:00am

    Originally posted by: Sergio A. Hernandez

    It seems that your code needs to be updated.  You can try this code if you want to only Select Multiple files.
    
    

    CString openFileDialogTitle = "Open File(s)";
    CString openFileDialogDefaultExtension = "*.*";
    CString openFileDialogFilterExtension = "All Files (*.*)|*.*||";

    CString file = "";
    CFileDialog * openFileDialog = NULL;
    openFileDialog = ( CFileDialog * )new CFileDialog( TRUE, openFileDialogDefaultExtension,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_FILEMUSTEXIST| OFN_EXPLORER|OFN_ALLOWMULTISELECT|OFN_LONGNAMES|OFN_ENABLESIZING, openFileDialogFilterExtension, this );

    /*--- Assign File Dialog Title. ---*/
    openFileDialog->m_ofn.lpstrTitle = openFileDialogTitle;
    TCHAR * tcharFile = new TCHAR[ 32767 ];
    ZeroMemory( tcharFile, 32767 );
    openFileDialog->m_ofn.lpstrFile = tcharFile;
    openFileDialog->m_ofn.nMaxFile = 32767;
    openFileDialog->m_ofn.nFileOffset = 0;

    /*-- Get Dialog Returned Dialog. ---*/
    int ret = openFileDialog->DoModal( );

    POSITION currentPos;
    if( IDOK == ret ) {

    currentPos = openFileDialog->GetStartPosition( );
    while( currentPos != NULL ) {

    file = openFileDialog->GetNextPathName(
    currentPos );
    AfxMessageBox( file );
    }
    }

    /*--- Clean up Memory. ---*/
    if( openFileDialog != NULL )
    delete openFileDialog;

    delete tcharFile;

    • Why does it lose files if you change directories?

      Posted by Mike Pliam on 01/27/2009 02:13pm

      Your code works nicely but if I change directories, it loses the files from any preceeding directories. Any way to fix that?

      Reply
    Reply
  • I agree this is not a good idea.....

    Posted by Legacy on 03/05/2003 12:00am

    Originally posted by: Richard 'vajuras' Osborne

    I'm sorry but Borland is right. Why not just use OFN_ALLOWMULTISELECT? What codeguru needs is a tut on how to use CFileDialog.....

    Reply
  • Why can not select only one file

    Posted by Legacy on 01/19/2003 12:00am

    Originally posted by: suvn.w

    It works well but when I only select one file, it cann't work properly, why?

    Reply
  • dude this seems pretty silly

    Posted by Legacy on 02/21/2002 12:00am

    Originally posted by: borland

    you do know you can just call CFileDialog with parameters of OFN_ALLOWMULTISELECT, etc and it will let you pick more than one file. Inheriting a class on top of it is kinda silly don't you think?

    Reply
  • Looking for the same

    Posted by Legacy on 04/27/2001 12:00am

    Originally posted by: Shrinivas

    Thanks. I was looking for the same. thanks again - shrini

    Reply
  • Not bad.

    Posted by Legacy on 12/20/2000 12:00am

    Originally posted by: Srinivasa

    Its cool way of overridding CFileDialog behaviour.

    Reply
  • ThanX

    Posted by Legacy on 05/17/2000 12:00am

    Originally posted by: kabir

    thanx for that, it solved a problem in less time than it would have taken

    Reply
  • m_ofn.nMaxFile = 2048; // arbitrary limit?

    Posted by Legacy on 01/11/2000 12:00am

    Originally posted by: Malcolm Ross

    Hi there,

    I see that in your class you assign a buffer of 2048 characters for storing the list of filenames. Is 2048 just an arbitrarily chosen number, or did you choose 2048 for a special reason? I understand that for multiple filenames you'll need more characters than just _MAX_PATH, but is there a limit to how large a buffer you can assign?

    Cheers,

    Mal.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds