MFC: MDI singleton doc/view application

·2 min read·paskov

The aim is to keep a single Contacts document/view window open in an MDI application. Reopening it should activate the existing window. This example uses two helper methods.

First, add a public method to your CDocument-derived class to check whether the document already has a view of the requested runtime class:

/**
 * @brief Checks whether a view of the given runtime class needs to be created.
 * @param rtClass The runtime class of the requested view.
 * @return FALSE if an existing view was activated; TRUE if a new view is needed.
 */
BOOL CVPhoneBookDoc::NeedViewCreation(CRuntimeClass* rtClass)
{
    POSITION pos = GetFirstViewPosition();
    while (pos != NULL)
    {
        CView* pView = GetNextView(pos);
        if (pView->GetRuntimeClass() == rtClass)
        {
            CMDIChildWnd* pViewFrame = (CMDIChildWnd*)pView->GetParentFrame();
            ((CMDIFrameWnd*)AfxGetMainWnd())->MDIActivate(pViewFrame);
            return FALSE; // Existing view activated.
        }
    }

    return TRUE; // A new view is needed.
}

The method iterates through the document’s views and compares their runtime classes with rtClass. If it finds an exact match, it activates the view’s MDI child frame and returns FALSE. Otherwise, it returns TRUE to indicate that a new view is needed.

Next, add a command handler to your CWinApp-derived class. Declare it in the application class and map the menu or toolbar command that opens the Contacts window to it. This example looks for a document template whose CDocTemplate::docName string is Contacts:

void CVPhoneBookApp::OnCreateContactView()
{
    POSITION curTemplatePos = GetFirstDocTemplatePosition();

    while (curTemplatePos != NULL)
    {
        CDocTemplate* curTemplate =
            GetNextDocTemplate(curTemplatePos);
        CString str;
        curTemplate->GetDocString(str, CDocTemplate::docName);
        if (str == _T("Contacts"))
        {
            // Look for an existing document in the Contacts template.
            POSITION docPos = curTemplate->GetFirstDocPosition();
            if (docPos == NULL)
            {
                curTemplate->OpenDocumentFile(NULL);
            }
            else
            {
                CVPhoneBookDoc* pDoc = (CVPhoneBookDoc*)curTemplate->GetNextDoc(docPos);
                if (pDoc)
                {
                    // Activate the existing view or allow a new window.
                    if (pDoc->NeedViewCreation(RUNTIME_CLASS(CContactsView)))
                    {
                        curTemplate->OpenDocumentFile(NULL);
                    }
                }
            }
        }
    }
}

The handler first checks whether the Contacts template has a document. If none exists, it calls CDocTemplate::OpenDocumentFile(NULL) to create one.

If a document already exists, the handler checks the template’s first document for a CContactsView using NeedViewCreation(). The helper activates an existing view; otherwise, the handler opens a new document/view window.