Link to home
Start Free TrialLog in
Avatar of IssacJones
IssacJones

asked on

Multiple selections in OpenFile dialog

Hi

I have an MFC application. I want to be able to select multiple files in the OpenFile dialog. How did I get a list of the selected files? Also, how can I look at this list and call the serialize code for each file?

John
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
serialise code - what do you mean, you have a Serialise in your CDocument based class - is that what you want to call for each file?
From the help files:

CArchive does not have a base class.
Later you can load the objects from persistent storage, reconstituting them  in memory. This process of making data persistent is called "serialization."
You can think of an archive object as a kind of binary stream. Like an  input/output stream, an archive is associated with a file and permits the  buffered writing and reading of data to and from storage. An input/output stream  processes sequences of ASCII characters, but an archive processes binary object  data in an efficient, nonredundant format.
You must create a CFile object before you can  create a CArchive object. In addition, you must ensure that the archive's  load/store status is compatible with the file's open mode. You are limited to  one active archive per file.
When you construct a CArchive object, you attach it to an object of  class CFile (or a derived class) that represents an open file. You also  specify whether the archive will be used for loading or storing. A  CArchive object can process not only primitive types but also objects of  CObject-derived classes  designed for serialization. A serializable class usually has a Serialize  member function, and it usually uses the DECLARE_SERIAL and IMPLEMENT_SERIAL macros, as  described under class CObject.
The overloaded extraction (>>) and insertion (<<)  operators are convenient archive programming interfaces that support both  primitive types and CObject-derived classes.
CArchive also supports programming with the MFC Windows Sockets  classes CSocket and CSocketFile. The IsBufferEmpty member  function supports that usage.
For more information on CArchive, see the articles Serialization and Windows Sockets: Using Sockets  with Archives.
You must set OFN_ALLOWMULTISELECT flag first.
Give a look at MSDN CFileDialog:
http://msdn.microsoft.com/en-us/library/dk77e5e7.aspx


// Create dialog to open multiple files.
CFileDialog dlg(TRUE, _T("txt"), _T("*.txt"), OFN_ALLOWMULTISELECT);

// Create buffer for file names.
const DWORD numberOfFileNames = 100;
const DWORD fileNameMaxLength = MAX_PATH + 1;
const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;
TCHAR* filenamesBuffer = new TCHAR[bufferSize];

// Initialize beginning and end of buffer.
filenamesBuffer[0] = NULL;
filenamesBuffer[bufferSize-1] = NULL;

// Attach buffer to OPENFILENAME member.
dlg.m_ofn.lpstrFile = filenamesBuffer;
dlg.m_ofn.nMaxFile = bufferSize;

// Create array for file names.
CString fileNameArray[numberOfFileNames];
if(dlg.DoModal() == IDOK)
{
    // Retrieve file name(s).
    POSITION fileNamesPosition = dlg.GetStartPosition();
    int iCtr = 0;
    while(fileNamesPosition != NULL)
    {
        fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition);
        iCtr++;
    }  
}
// Release file names buffer.
delete[] filenamesBuffer;

Open in new window

Avatar of IssacJones
IssacJones

ASKER

Hi Andy

What I mean by serialize is that once I know the name of the files selected (answer given by your first post) how do I load these files in turn? For example, I want to load the first file, do some work on it, load the second, do some work on it etc.

John
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For example, I want to load the first file, do some work on it, load the second, do some work on it etc.

You can use CArchive - see the extract from help I posted before.  You could open the files directly with CFile or CStdioFile.
It all depends on whatyou want to do, what format the files are in...
Andy, the files have been created from the application i.e. the document has serialization code which loads and saves the files. All I need to do is call this code, to load the files, in succession.
You could base a CArchive on each file in turn, then pass this onto the CDocument based class's Serialize function.
Hi Andy

"You could base a CArchive on each file in turn, then pass this onto the CDocument based class's Serialize function."

Could you give some example code?

John
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial