[MFC] Inlocuirea unui view la run-time

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
nmg
Junior
Junior
Posts: 43
Joined: 12 Jul 2007, 07:36

[MFC] Inlocuirea unui view la run-time

Post by nmg » 01 Aug 2007, 09:18

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
Last edited by Ovidiu Cucu on 08 Aug 2011, 19:02, edited 6 times in total.
Reason: structura intrebare/raspuns



Post Reply