Click here to Skip to main content
16,020,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am creating an sample learning application to create multiple active views at the same time in an SDI. The problem that i am facing is, though i have overridden the OnDraw in my second view class, my call to the SetModifiedFlag(true) and UpdateAllViews(), is not invoking the second views OnDraw(). I debugged into the UpdateAllviews and i could see my second view's OnUpdate is getting called successfully with all parameters 0. What is it that i am missing? please can any one help

Code looks something like this
CMainWnd::CMainWnd()
{
    m_pSecondWnd = new CSecondaryWnd();
    m_pSecondWnd->Create( 0, L"Second Window", WS_OVERLAPPEDWINDOW );
}
void CMainWnd::CreateNewView( CRuntimeClass* pView, CFrameWnd* pNewWnd, UINT ID)
{
    CView* pNewView = NULL;
    pNewView = (CView*)pView->CreateObject();
    CCreateContext context;
    context.m_pCurrentDoc = GetActiveDocument();
    pNewView->Create( 0, 0, AFX_WS_DEFAULT_VIEW, rectDefault, pNewWnd, 1 , &context );
    pNewView->OnInitialUpdate();

    ((CSecondaryWnd*)pNewWnd)->ShowWindow( SW_RESTORE );
    pNewView->ShowWindow( SW_RESTORE );
    pNewWnd->UpdateWindow();
}

void CMainWnd::OnTimer( UINT_PTR nIDEvent )
{
    switch(nIDEvent)
    {
    case ONE_SECOND_TIMER:
        {
            CSampleDocument* pDoc = (CSampleDocument*)GetActiveDocument();
            pDoc->SetModifiedFlag();
            pDoc->UpdateAllViews( NULL );
            //Invalidate(); trail
            break;
        }
    }
}

Sample View 2 class:

IMPLEMENT_DYNCREATE( CSampleView2, CView )

BEGIN_MESSAGE_MAP( CSampleView2, CView )
END_MESSAGE_MAP()

CSampleView2::CSampleView2()
{
}

CSampleView2::~CSampleView2()
{
}


void CSampleView2::OnDraw( CDC* pDc )
{
    int a = 10;
    pDc->TextOut( 100, 100, L"This", 5 );
}


Sample View 1 Class:
IMPLEMENT_DYNCREATE( CSampleView1, CView )

BEGIN_MESSAGE_MAP( CSampleView1, CView )
    ON_COMMAND( ID_VIEW_CREATE, OnViewCreate )
END_MESSAGE_MAP()

CSampleView1::CSampleView1()
{
}

CSampleView1::~CSampleView1()
{
}

void CSampleView1::OnDraw( CDC* pDc )
{
    pDc->TextOut( 100, 100, L"This", 5 );int a = 10;
}


What I have tried:

Debugged UpdateAllView and i could see while looping through the different views, my second view calss and it OnUpdate is getting called successfully.
Posted
Updated 12-Sep-17 20:31pm

1 solution

See CDocument::UpdateAllViews (MFC)[^] for the correct method of updating views.
 
Share this answer
 
Comments
kuttiylajai 13-Sep-17 15:59pm    
I read the documentation CDocument::UpdateAllViews (MFC)[^] and it was mentioning that "You typically call this function from your view class after the user has changed the document through a view."
I changed the code as per that to
void CSampleView1::OnViewCreate()
{
CRuntimeClass* second_view = RUNTIME_CLASS( CSampleView2 );
CMainWnd* pMainWnd = (CMainWnd*)AfxGetApp()->GetMainWnd();
pMainWnd->CreateNewView( second_view, pMainWnd->GetMainWnd(), 1 );
}

void CSampleView1::UpdateViewsToReflectChanges()
{
CSampleDocument* pDoc = (CSampleDocument*)((CFrameWnd*)(AfxGetApp()->GetMainWnd()))->GetActiveDocument();
pDoc->SetModifiedFlag();
pDoc->UpdateAllViews( NULL );
}
But the answer is same. I am not getting control in my OnDraw of the secondView class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900