CSplitterWnd動態分割

靜態分割不提了,網上一大堆。關鍵是動態分割要怎麼辦?
一、從未切分到切分
二、從切分到未切分
三、從切分狀態m到切分狀態n的轉變
好比這裏要實現一個通達信的看盤窗口,分紅主窗口和指標窗口。指標窗口可隨時關閉,也可隨時打開。this

通過屢次嘗試,最終肯定如下辦法指針

在OnCreateClient中,動態建立目前將要展現的視圖,而且保存document指針code

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
 CRect rect;
 GetClientRect(&rect);
 CView* view = new CMFCApplication4View();
 view->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rect,this,AFX_IDW_PANE_FIRST,pContext);
 m_pDocument = pContext->m_pCurrentDoc;
 return TRUE;
}

切換到分割視圖的時候,先將原來的窗口設置id爲一個非AFX_IDW_PANE_FIRST的數字,而後動態建立分割,以後將原來的窗口Destroy掉,而且調用InitialUpdateFrame使得新建立的內部視圖能夠使用document支持。繼承

void CMainFrame::OnSplit()
{
 
 CWnd* oldView = GetDlgItem(AFX_IDW_PANE_FIRST);
 oldView->SetDlgCtrlID(1000);
 
 createSplitter(oldView);
 oldView->DestroyWindow();

 GetDlgItem(AFX_IDW_PANE_FIRST)->GetParentFrame()->InitialUpdateFrame( m_pDocument, TRUE );
}
void CMainFrame::createSplitter(CWnd* oldView)
{
 CAutoDeleteSplitterWnd* splitter = new CAutoDeleteSplitterWnd;
 if(!splitter->CreateStatic(this,2,1,WS_CHILD|WS_VISIBLE,AFX_IDW_PANE_FIRST)){
  return;
 }

 CCreateContext context;
 context.m_pCurrentDoc =m_pDocument;
 context.m_pCurrentFrame = this;

 splitter->CreateView(0,0,RUNTIME_CLASS(CMyView),CSize(0,100),&context);
 splitter->CreateView(1,0,RUNTIME_CLASS(CMFCApplication4View),CSize(0,100),&context);

 CRect rect;
 oldView->GetWindowRect(&rect);
 ScreenToClient(&rect);
 splitter->MoveWindow(&rect,TRUE);
}
CAutoDeleteSplitterWnd 是一個繼承CSplitterWndEx的類,關鍵在於
void CAutoDeleteSplitterWnd::OnNcDestroy()
{
 
 delete this;
}

切換回來的代碼與前面一模一樣。it

void CMainFrame::OnNotSplit()
{
 CWnd* oldView = GetDlgItem(AFX_IDW_PANE_FIRST);
 oldView->SetDlgCtrlID(1000);

 createSingleView(oldView);
 oldView->DestroyWindow();
 GetDlgItem(AFX_IDW_PANE_FIRST)->GetParentFrame()->InitialUpdateFrame( m_pDocument, TRUE );
}
void CMainFrame::createSingleView(CWnd* oldView)
{
 CCreateContext context;
 context.m_pCurrentDoc =m_pDocument;
 context.m_pCurrentFrame = this;
 context.m_pNewViewClass = RUNTIME_CLASS(CMFCApplication4View);

 CRect rect;
 GetClientRect(&rect);
 CView* view = new CMFCApplication4View();
 view->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rect,this,AFX_IDW_PANE_FIRST,&context);
 
}
相關文章
相關標籤/搜索