I created different views of one document using a MultiDocTemplate.
How do I delete all the views automatically when one view is closed?
Is there another way to create these different views of one and the same
document dynamically?
Thanks
Marco
void CDocument::OnCloseDocument()
// must close all views now (no prompting) - usually destroys
this
{
// destroy all frames viewing this document
// the last destroy may destroy us
BOOL bAutoDelete = m_bAutoDelete;
m_bAutoDelete = FALSE; // don't destroy document while
closing views
while (!m_viewList.IsEmpty())
{
// get frame attached to the view
CView* pView = (CView*)m_viewList.GetHead();
ASSERT_VALID(pView);
CFrameWnd* pFrame = pView->GetParentFrame();
ASSERT_VALID(pFrame);
// and close it
PreCloseFrame(pFrame);
pFrame->DestroyWindow();
// will destroy the view as well
}
m_bAutoDelete = bAutoDelete;
// clean up contents of document before destroying the document
itself
DeleteContents();
// delete the document if necessary
if (m_bAutoDelete)
delete this;
}
Scot T Brennecke schrieb:
OnCloseDocument
1.) Definition of the template and entry in the m_MapView, which is a map storing
the pointers to my templates:
BOOL CGuiApp::InitInstance()
{
.
.
.
pDocTemplate = new CMultiDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGuiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CDimMatrixView));
AddDocTemplate(pDocTemplate);
m_MapView.SetAt("CDimMatrixView", pDocTemplate);
.
.
.
}
2.) Creation of the "DimMatrixView" in my "GuiView" class (using a map-lookup to
retrieve the template):
void CGuiView::OnDisplayDimensionalmatrix()
{
.
.
.
CGuiApp *pApp = (CGuiApp*)AfxGetApp();
CMultiDocTemplate* pa;
if (pApp->m_MapView.Lookup("CDimMatrixView", (CObject *&) pa ))
{
CFrameWnd *pFrame = pa->CreateNewFrame(GetDocument(), GetParentFrame());
pFrame->InitialUpdateFrame(GetDocument(), TRUE);
}
.
.
.
}
3.) Deleting all views of one document out of my "GuiView" class:
void CGuiView::OnDestroy()
{
CScrollView::OnDestroy();
// TODO: Code für die Behandlungsroutine für Nachrichten hier einfügen
GetDocument()->OnCloseDocument();
}
4.) Exception in OnCloseDocument():
Destroying the first frame is no problem.
In the second cycle of line 8-20, pView in line 11 is the same pointer as in
the first cycle.
Therefore I get a NULL pFrame pointer in line 13, which causes the exception in
line 18.
1 void CDocument::OnCloseDocument()
2 // must close all views now (no prompting) - usually destroys this
3 {
4 // destroy all frames viewing this document
5 // the last destroy may destroy us
6 BOOL bAutoDelete = m_bAutoDelete;
7 m_bAutoDelete = FALSE; // don't destroy document while closing views
8 while (!m_viewList.IsEmpty())
9 {
10 // get frame attached to the view
11 CView* pView = (CView*)m_viewList.GetHead();
12 ASSERT_VALID(pView);
13 CFrameWnd* pFrame = pView->GetParentFrame();
14 ASSERT_VALID(pFrame);
15
16 // and close it
17 PreCloseFrame(pFrame);
18 pFrame->DestroyWindow();
19 // will destroy the view as well
20 }
21 m_bAutoDelete = bAutoDelete;
22
23 // clean up contents of document before destroying the document itself
24 DeleteContents();
25
26 // delete the document if necessary
27 if (m_bAutoDelete)
28 delete this;
29 }
I hope, this is enough to give you an idea of my problem.
Any suggestions for fixing it?
Scot T Brennecke schrieb:
Thanks a bomb!
Marco
Scot T Brennecke schrieb: