Open multiple documents from a single file
-
03-07-2019 - |
Question
How would I go about creating multiple documents when a single file is opening in a MFC application?
We have an aggregate file format which can contain information for multiple documents. When this file is opened, I would like multiple CDocuments created for each record in the file. We already have a extended CDocManager, so I'm guessing this could be implemented by some logic in OpenDocumentFile. The question is how to pass the information about "I am record x of y" back up from the CDocument class to the doc manager?
Solution
If you have several CDocument derived types that store different information, you need a seperate CMultiDocTemplate for each type, typically stored in your CApp derived class. When you call App.Initinstance you initialise each template using something like
m_MyTempate1 = new CAtlasMDITemplate(IDR_RES_TYPE1,
RUNTIME_CLASS(CDocumentType1),
RUNTIME_CLASS(CChildFrameSplitter),
RUNTIME_CLASS(CViewType1));
When you open your base document, you then create and retrieve your additional documents. There are a number of places you could do this in, Serialize probably being the easiest, e.g.
void CDocumentType1::Serialize(CArchive& ar)
{
//
// Do all the document type 1 serialisation
//
// Create second doc
CDocumentType2 *pDoc2 = theApp.m_MyTempate2->OpenDocumentFile(NULL);
pDoc2->Serialize(ar);
}
The more conventional way of doing this though would be to have a single document, with multiple views for accessing the different parts of the aggregate document.