Discussion:
MDI app with multiple views
(too old to reply)
R.Krishnan
2006-05-25 18:34:59 UTC
Permalink
I have a MDI app with multiple views. I want to have exactly one view of a
particular type. If a user clicks on the view menu (attached to mainfrm),
she will be taken to the existing view if it exists, else a new view of that
type will be created. Here is part of the code that tries to do this.
Unfortunately nothing happens (the window does not get updated at all). Am
I making a mistake getting the frame window of the view that I wish to see
(if it exists). Is there a better way of doing what I am trying to do? TIA

void CMainFrame::OnUserinput()
{
//get the active child
CMDIChildWnd * pActiveChild = MDIGetActive();
//get the current view (the one we want replaced)
CView * pCurrView = pActiveChild->GetActiveView();
CDOptimizerApp * pApp = (CDOptimizerApp*)AfxGetApp();
CDocument * pDocument = pActiveChild->GetActiveDocument();
POSITION posView = pDocument->GetFirstViewPosition();
while (posView != NULL )
{
CView *pView = pDocument->GetNextView( posView );
if (pView->IsKindOf(RUNTIME_CLASS(CUserInputView))) //this is the
replacement view, and we check if it already exists
{
//get the parent frame
CFrameWnd * pFrame = pView->GetParentFrame(); //is this line the
problem?
//set the new view as the active view
pFrame->SetActiveView(pView);
//hide the old view
pCurrView->ShowWindow(SW_HIDE);
//show the new view
pView->ShowWindow(SW_SHOW);
return;
}
}
//otherwisse create a new view
if (pActiveChild != 0)
{
CDocument * pDocument = pActiveChild->GetActiveDocument();
if (pDocument != 0)
{
CDOptimizerApp * pApp = (CDOptimizerApp*)AfxGetApp();
CDocTemplate * pTemp;
CFrameWnd * pFrame;
pTemp = pApp->GetUserInputTemplate();
pFrame = pTemp->CreateNewFrame(pDocument,pActiveChild);
if (pFrame != 0 )
{
pTemp->InitialUpdateFrame(pFrame,pDocument);
}
}
}
}
www.fruitfruit.com
2006-06-27 09:30:04 UTC
Permalink
Post by R.Krishnan
I have a MDI app with multiple views. I want to have exactly one view of a
particular type. If a user clicks on the view menu (attached to mainfrm),
she will be taken to the existing view if it exists, else a new view of that
type will be created. Here is part of the code that tries to do this.
Unfortunately nothing happens (the window does not get updated at all). Am
I making a mistake getting the frame window of the view that I wish to see
(if it exists). Is there a better way of doing what I am trying to do? TIA
void CMainFrame::OnUserinput()
{
//get the active child
CMDIChildWnd * pActiveChild = MDIGetActive();
//get the current view (the one we want replaced)
CView * pCurrView = pActiveChild->GetActiveView();
CDOptimizerApp * pApp = (CDOptimizerApp*)AfxGetApp();
CDocument * pDocument = pActiveChild->GetActiveDocument();
POSITION posView = pDocument->GetFirstViewPosition();
while (posView != NULL )
{
CView *pView = pDocument->GetNextView( posView );
if (pView->IsKindOf(RUNTIME_CLASS(CUserInputView))) //this is the
replacement view, and we check if it already exists
{
//get the parent frame
CFrameWnd * pFrame = pView->GetParentFrame(); //is this line the
problem?
//set the new view as the active view
pFrame->SetActiveView(pView);
//hide the old view
pCurrView->ShowWindow(SW_HIDE);
//show the new view
pView->ShowWindow(SW_SHOW);
return;
}
}
//otherwisse create a new view
if (pActiveChild != 0)
{
CDocument * pDocument = pActiveChild->GetActiveDocument();
if (pDocument != 0)
{
CDOptimizerApp * pApp = (CDOptimizerApp*)AfxGetApp();
CDocTemplate * pTemp;
CFrameWnd * pFrame;
pTemp = pApp->GetUserInputTemplate();
pFrame = pTemp->CreateNewFrame(pDocument,pActiveChild);
if (pFrame != 0 )
{
pTemp->InitialUpdateFrame(pFrame,pDocument);
}
}
}
}
Need to call ActivateFrame(), see the following code snippet
void CMainFrame::CreateOrActivateFrame(CDocTemplate*
pTemplate,CRuntimeClass* pViewClass)
///////////////////////////////////////
// pDocumentTemplate is a CDocTemplate* to the template you wish to use
// which is passed into this function.
// pViewClass is a CRuntimeClass* to the view class desired which is
// passed into the function
{
// Get the currently active child.
CMDIChildWnd* pMDIActive = MDIGetActive();
ASSERT(pMDIActive != NULL);
if(pMDIActive == NULL)
return;
// now get that child's document... you may not need to do this, but if
there may
// be multiple documents this a way to make sure that the correct set
of windows
// is being acted on.
CDocument* pDoc = pMDIActive->GetActiveDocument();
ASSERT(pDoc != NULL);
CView* pView;
POSITION pos = pDoc->GetFirstViewPosition();
while (pos != NULL)
{
pView=pDoc->GetNextView(pos);
if(pView->IsKindOf(pViewClass))
{ // We found a view for this alreadycreated.
pView->GetParentFrame()->ActivateFrame();
return;
}
}
// This view doesn't exist for this document, create it
CMDIChildWnd* pNewFrame =
(CMDIChildWnd*)(pTemplate->CreateNewFrame(pDoc, NULL));
if (pNewFrame ==NULL)
return; // not created you may want to assert here
ASSERT_KINDOF(CMDIChildWnd,pNewFrame);pTemplate->InitialUpdateFrame(pNewFrame,
pDoc);
}

Loading...