2012-04-21 70 views
0

我創建了一個MFC MDI應用程序,並希望通過右鍵單擊並選擇一個「AddSplitWnd」彈出菜單項來動態地將窗口分成兩部分。我嘗試使用CSplitterWnd :: CreateStatic來實現它,一旦窗口被拆分,它需要創建一個新的視圖,但我想使用前一個視圖,所以沒有人知道如何實現它。謝謝。如何在不使用CSplitterWnd的情況下在MFC中動態分割窗口::創建

回答

0

這是一個代碼片段,用於在SDI環境中的分離器中交換視圖。這應該適用於MDI的工作。

CView* CDoc::SwitchToView(CView* pNewView) 
{ 
    CFrameWndEx* pMainWnd = (CFrameWndEx*)AfxGetMainWnd(); 
    CView* pOldActiveView; 
    pOldActiveView = pMainWnd->GetActiveView(); 
    CSplitterWnd* pSplitter = (CSplitterWnd *)pOldActiveView->GetParent(); 

    // in this case Pane 0,0 is exchanged 
    pOldActiveView = (CView*) pSplitter->GetPane(0,0); 

    // set flag so that document will not be deleted when view is destroyed 
    m_bAutoDelete = FALSE;  
    // Dettach existing view 
    RemoveView(pOldActiveView); 
    // set flag back to default 
    m_bAutoDelete = TRUE; 

    // Set the child window ID of the active view to the ID of the corresponding 
    // pane. Set the child ID of the previously active view to some other ID. 
    ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, 0); 
    ::SetWindowLong(pNewView->m_hWnd, GWL_ID, pSplitter->IdFromRowCol(0,0)); 

    // Show the newly active view and hide the inactive view. 
    pNewView->ShowWindow(SW_SHOW); 
    pOldActiveView->ShowWindow(SW_HIDE); 

    // Attach new view 
    AddView(pNewView); 

    // Set active 
    pSplitter->GetParentFrame()->SetActiveView(pNewView); 
    pSplitter->RecalcLayout(); 
    return pOldActiveView; 
} 

HTH

+0

嗨DWO,感謝您的answer.The核心思想是設置新視圖的控件ID爲老活動視圖的ID。但是當我嘗試這樣做時會遇到一些問題。我通過CSplitterWnd :: CreateView()創建一個新的視圖。假設我有一個由m_splitter1創建的分割窗口,它有兩個視圖pView1和pView2,下一次我在pView1上創建另一個分割窗口,並且在創建pView3時想要創建一個pView3,pView4,我總是很奇怪獲得與pView1相同的地址,但是pView1的成員已經被重新初始化,所以我不能再次獲取它。你遇到過這樣的情況嗎? – ohluyao 2012-04-24 11:38:03

+0

在沒有CSplitterWnd :: CreateView的情況下創建視圖。只需使構造函數公開並自己調用new,Create和InitialUpdate即可。 – dwo 2012-04-24 18:01:41

+0

謝謝,我會試一試。 – ohluyao 2012-04-25 14:19:21

相關問題