Page 1 of 1

[MFC] Inlocuirea unui view la run-time

Posted: 01 Aug 2007, 09:18
by nmg
Intrebare: Intr-o aplicatie SDI, cateodata trebuie sa schimbam modul in care este prezentata informatia.
De exemplu, ne trebuie un list view (derivat din CListView) in loc de un edit view (derivat din CEditView). Cum se face asta?

Raspuns: Urmatoarea functie inlocuieste vechiul view cu unul nou, creat "din mers":

Code: Select all

void CMainFrame::ChangeView(CRuntimeClass *pNewView)
{
    // get current view:
    CView* pOldView = GetActiveView();
    
    // set new view
    CCreateContext context;
    context.m_pNewViewClass = pNewView;
    context.m_pCurrentDoc = GetActiveDocument();

    // create new view:
    CView* pNew = STATIC_DOWNCAST(CView , CreateView(&context));
    SetActiveView(pNew);
    pNew->ShowWindow(SW_SHOW);

    pNew->OnInitialUpdate(); 
    pNew->SetFocus();

    // just in case we have some controls to arrange:
    pNew->PostMessage(WM_SIZE);

    // get rid of old view:
    pOldView->DestroyWindow();
}
Functia va fi apelata astfel:

Code: Select all

ChangeView (RUNTIME_CLASS(CTheNewView))

<< Back to MFC index