静态分割不提了,网上一大堆。关键是动态分割要怎么办?
一、从未切分到切分
二、从切分到未切分
三、从切分状态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); }