2016-09-21 84 views
0

我對MFC應用程序有疑問。 現在,我維護傳統的MFC項目。還有一個巨大的問題。 我們沒有爲視圖類使用文檔類。例如,有一個由3個視圖組成的子框架。其中三人使用幾乎相同的數據。但是每個視圖不是從文檔中自行獲取數據。所以有很多重複的代碼。使用MDI應用程序的Childframe中的一個文檔的多個視圖

我想解決這個問題和重構。所以我搜索瞭如何將視圖和文檔鏈接到子框架中。所有的樣本都是關於CMultiDocTemplate構造函數的。以下是我試過的。

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); 

如果我用像上面,它問你要顯示的幀。這不是我想要的。這是一個不同的框架。

我想在同一個子框架中製作多個視圖和一個文檔。 我也試過這種方式。

CMultiDocTemplate* pDocTemplate; 
pDocTemplate = new CMultiDocTemplate(IDR_MFCApplication3TYPE, 
    RUNTIME_CLASS(CMFCApplication3Doc), 
    RUNTIME_CLASS(CChildFrame), 
    NULL); 
if (!pDocTemplate) 
    return FALSE; 
AddDocTemplate(pDocTemplate); 

NULL for a view。並在子框架的創建視圖的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); 
} 

它可以很好地創建多個視圖。但我不知道如何鏈接CTestView,CTestView2和一個文檔。 當我在TestView中訪問文檔類時,我只能訪問基本文檔類的CDocument。 我想要處理像CTestDocument這樣的特定文檔。

有什麼辦法嗎?如果有,請讓我知道。

感謝您閱讀我的問題。

回答

1

當視圖被創建時,它所屬的文檔被傳入CCreateContext。

文檔模板具有簡單功能CMultiDocTemplate :: CreateNewFrame。使用此功能,您可以使用現有模板創建新的框架/視圖組合。

還有就是功能的CFrameWnd :: CreateView的服用CCreateContext ...

0

這是在MFC中常見的做法是一個GetDocument()成員添加到的意見。

//.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 

您將需要使用CTestDocument更改CMFCApplication3Doc,因此它是爲您的框架打開的文檔。

相關問題