0
votes

I am using the MFC classes, but in a non-standard way (i.e. not using a CDocument derived class at all).

Be that as it may, I have several views derived from CFormView, and there are class member variables.

I have code that uses a derived class from CMiniFrameWnd and it is like so

CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CImageView);
context.m_pCurrentDoc = NULL;
CView* pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
if (pNewView != NULL)
{  
    pNewView->ShowWindow(SW_SHOW);
    pNewView->OnInitialUpdate();
    SetActiveView(pNewView);
}
// save view

But the snag with this is CreateView() is calling the default constructor of CImageView, and OnInitialUpdate() being a virtual override, must match CView()'s signature.

So how do I initialise member data belonging to CImageView? CreateView() and OnInitialUpdate() get in the way (unless I am missing something). It seems that derived classes from CView or CFormView are not easily initialised in the MFC architecture.

Thanks

1

1 Answers

0
votes

I am going to confess that I am not sure this is what you want but in my application I have to convert to a different view type. I do it like this:

BOOL CCommunityTalksDoc::SwitchToView(CRuntimeClass* pNewViewClass)
{
    POSITION        rPos;
    CView           *pOldActiveView;
    CFrameWnd       *pChild;
    CCreateContext  context;
    BOOL            bAutoDelete;

    rPos = GetFirstViewPosition();
    pOldActiveView = GetNextView(rPos);
    pChild = pOldActiveView->GetParentFrame();

    // If we're already displaying this kind of view, no need to go further.
    if (pOldActiveView->IsKindOf(pNewViewClass))
        return TRUE;

    // Set flag so that document will not be deleted when view is destroyed.
    bAutoDelete = m_bAutoDelete;
    m_bAutoDelete = FALSE;
    // Delete existing view
    pOldActiveView->DestroyWindow();
    // restore flag
    m_bAutoDelete = bAutoDelete;

    // Create new view.
    m_pScriptView = (CScriptParseView*)pNewViewClass->CreateObject();
    if (m_pScriptView == nullptr)
    {
        TRACE1("Warning: Dynamic create of view type %s failed\n", pNewViewClass->m_lpszClassName);
        return FALSE;
    }

    // we must ensure the popup dialogues don't display
    m_pScriptView->SetBuildMode(FALSE);

    // Draw new view.
    context.m_pNewViewClass = pNewViewClass;
    context.m_pCurrentDoc = this;
    context.m_pNewDocTemplate = nullptr;
    context.m_pLastView = nullptr;
    context.m_pCurrentFrame = pChild;
    if (!m_pScriptView->Create(nullptr, nullptr, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), 
                pChild, AFX_IDW_PANE_FIRST, &context))
    {
        TRACE0("Warning: couldn't create view for frame\n");
        delete m_pScriptView;
        m_pScriptView = nullptr;
        return FALSE; 
    }

    m_pScriptView->SendMessage(WM_INITIALUPDATE, 0, 0);  // WM_INITIALUPDATE is defined in afxpriv.h
    pChild->RecalcLayout();
    m_pScriptView->UpdateWindow();
    pChild->SetActiveView(m_pScriptView);
    return TRUE;
}

The calling code:

// we must set our view correctly
// note, I am not convinced we are doing this in the right place
// what happens if user makes a second view?
// and what about when RebuildReport is called ?
if(m_pScriptView == nullptr)
{
    //bNewReport = true;
    // we must create view
    pNewViewClass = RUNTIME_CLASS(CScriptParseView);
    if (!SwitchToView(pNewViewClass))
    {
        // fail, don't know why it would fail
        ASSERT(FALSE);
    }
    else
    {
        // we set the variables map only once
        m_pScriptView->SetVarValuesMap(psData->pMapSSVarsValues);
        m_pScriptView->SetVarOptionsMap(psData->pMapSSVarsOptions); // AJT v10.6.0
    }
}

Hope this helps.