Automatically create multiple doc/view in MDI in MFC
Hi.
How do I create multiple document/views at the start of the program?
I would prefer it as a doc/view list with IDs that I can access from other part of the program.
For example I have a MenuBar with "Create 2x2" and I would like to dynamically create 4 doc/views
in 2x2 matrix with each showing different data based on the doc that belongs to it.
Also how do I disable automatic creation of the first view?
Re: Automatically create multiple doc/view in MDI in MFC
Quote:
Originally Posted by
Odiee
Also how do I disable automatic creation of the first view?
I guess you mean the automatic open of the first doc (and the first view) which is usually done if no comannd line parameter has been given?
Then, in your app's class InitInstance function search for the call of "ParseCommandLine(cmdInfo);" and add a line after it:
Code:
ParseCommandLine(cmdInfo);
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
To create a new doc programmatically:
From any function of your app do the following:
Code:
POSITION pos = GetFirstDocTemplatePosition();
CDocTemplate* pT = GetNextDocTemplate(pos);
CDocument* pDoc = pT->OpenDocumentFile(NULL);
Note: This will create a new doc of the first doc template class. If you're using more then one doc templates (not by default on wizzard created MDI app's) you'll need to add some more stuff.
Hope that helps
With regards
Programartist
Re: Automatically create multiple doc/view in MDI in MFC
First, handle ID_FILE_NEW command in your CWinApp-derived class and do nothing inside.
Next, loop into document templates list and create the documents and frames as you wish.
Example:
Code:
void CMyMDIApp::OnOpenAllViews()
{
POSITION posDocTemplate = GetFirstDocTemplatePosition();
while(NULL != posDocTemplate)
{
CDocTemplate* pDocTemplate = GetNextDocTemplate(posDocTemplate);
POSITION posDoc = pDocTemplate->GetFirstDocPosition();
if(NULL == posDoc)
{
pDocTemplate->OpenDocumentFile(NULL);
}
}
}
[ Later edit ]
I've noticed that assigning CCommandLineInfo::FileNothing to cmdInfo.m_nShellCommand, suggested by ProgramArtist is more elegant than handling ID_FILE_NEW.
Re: Automatically create multiple doc/view in MDI in MFC
That's exactly what I was looking for.
Thanks!