1
votes

I would like to have multiple views for a document in my MDI MFC application. In order to do that, InitInstance of my App class has following code

m_pMainTemplate = new CMultiDocTemplate(IDR_OpenCVTestTYPE,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CImageView));
if (!m_pMainTemplate)
    return FALSE;
AddDocTemplate(m_pMainTemplate);

m_pHistTemplate = new CMultiDocTemplate(IDR_OpenCVTestTYPE,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CHistogramView));
if (!m_pHistTemplate)
    return FALSE;
AddDocTemplate(m_pHistTemplate);

But when I start the application, it keeps asking which document among two documents user wants to choose. Of course, those documents are the same kind. Any idea or hint to resolve this problem?

UPDATE: I resolved the problem by having separate menu for the second view and overriding OnFileNew like this

 void CMyApp::OnFileNew()
 {
    // TODO: Add your command handler code here
    m_pMainTemplate->OpenDocumentFile(NULL);
 }
1
Overriding OnFileNew is the right approach in your case.zar

1 Answers

2
votes

Don't add a second template.

This Microsoft KB article shows how to create multiple views for a single doc.

EDIT: One possibilty is to modify your document template before opening the child frame that must have the alternate view.

m_pMainTemplate->m_m_pNewViewClass = RUNTIME_CLASS(CHistogramView);

You should then probably set the view in the template before opening any kind of child frame in order to make sure that the template is in the expected state for every child frame opening.