Multiple File Selection Without Any Extra Code

Environment: DOC View

This is in response to the column “Multiple File Select in File Open Dialog” under the section “Document—View/Misc.”

Instead of creating a new class (derived from CDocManager), I felt that I could accomplish the same functionality in a simpler way. I have already implemented a code in which I get the file name from the user, using CFileDialog on “OnFileOpen”. This is the message handler for ID_FILE_OPEN. This could be done just by adding the style “OFN_ALLOWMULTISELECT” in the CFileDialog Constructor.

Here’s the code snippet:

void MYApp::OnFileOpen( )
{
  CString csRootPath( "E:\" );
  //Display "File Open"/"File SaveAs" Dialog depending
  //upon     //"p_bOpen"
  CFileDialog cfileDlg( p_bOpen ,NULL,NULL,OFN_ALLOWMULTISELECT,
                        "All files (*.*)|*.*|");

  CString csFileDlgTitle;
  csFileDlgTitle.LoadString( "File Open" );

  cfileDlg.m_ofn.lpstrTitle = csFileDlgTitle;
  cfileDlg.m_ofn.lpstrInitialDir = csRootPath;

  int nRet = cfileDlg.DoModal( );
  ASSERT( ( nRet != IDOK ) || ( nRet != IDCANCEL ) );
}

Fine. How do you get the selected file name(s)?

Simple. Use GetStartPosition() and GetNextPathName() of CFileDialog to iterate the selected file names.

Here’s the code snippet:

POSITION pos ( cfileDlg.GetStartPosition() );

while( pos )
{
  CString csFileName(  cfileDlg.GetNextPathName( pos ) );
  //implement ur fn to open the file accordingly
}

With Regards,
Sriram

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read