Reputation: 3
I have a questions about MFC application. Nowadays I'm maintaining legacy MFC project. And there is a huge problem. We are not using document class for view class. so for example, There is a child frame that made of 3 views. Three of them use almost same data. But each views get the data by themselves not from document. so there are a lot of duplicated code.
I want to solve this and refactor. So I searched for how to link views and doc in a child frame. And all samples were about CMultiDocTemplate constructor. following is what I tried.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_MFCApplication3TYPE,
RUNTIME_CLASS(CMFCApplication3Doc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CMFCApplication3View));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
pDocTemplate = new CMultiDocTemplate(IDR_MFCApplication3TYPE,
RUNTIME_CLASS(CMFCApplication3Doc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(MyTreeView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
If I used like above, it asked which frame you want to show. It wasn't not what I wanted. It was a different frame.
I want to make multiple views and one document in a same child frame. And I also tried this way.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_MFCApplication3TYPE,
RUNTIME_CLASS(CMFCApplication3Doc),
RUNTIME_CLASS(CChildFrame),
NULL);
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
NULL for a view. And create views in a child frame's OnCreateClient()
BOOL CChildFrm::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
m_wndSplitter1.CreateStatic(this, 2, 1);
m_wndSplitter2.CreateStatic(&m_wndSplitter1, 1, 2);
m_wndSplitter3.CreateStatic(&m_wndSplitter2, 2, 1);
m_wndSplitter3.CreateView(1,0 , RUNTIME_CLASS(CTestView), CSize(200, 300), pContext);
m_wndSplitter3.CreateView(0,0 , RUNTIME_CLASS(CTestView2), CSize(200, 200), pContext);
}
It can create multiple views well. But I don't know how to link CTestView,CTestView2 and one document. When I approach document class in a TestView, I only can approach to CDocument that is base document class. I want to approach specific document like CTestDocument.
Is there any way?? If there, Please let me know.
Thanks for reading my questions.
Upvotes: 0
Views: 1312
Reputation: 1939
It is a common practice in MFC to add a GetDocument( ) member to views.
//.h
#ifndef _DEBUG
CTestDocument* GetDocument( ) { return dynamic_cast< CTestDocument* >( CView::GetDocument( ) ); }
#else
CTestDocument* GetDocument( );
#endif
//.cpp
#ifdef _DEBUG
CTestDocument* RaRichView::GetDocument( )
{
assert( dynamic_cast< CTestDocument* >( CView::GetDocument( ) ) );
return dynamic_cast< CTestDocument* >( CView::GetDocument( ) ); }
#endif
You will want to change CMFCApplication3Doc with CTestDocument so it is the document opened for your frame.
Upvotes: 0
Reputation: 15375
When a view is created the document it belongs to is passed in the CCreateContext.
The document template has the simple function CMultiDocTemplate::CreateNewFrame. With this function you create a new frame/view combination with an existing template.
Also there is the function CFrameWnd::CreateView taking a CCreateContext...
Upvotes: 1