静态分割不提了,网上一大堆。关键是动态分割要怎么办?
1、从未切分到切分
2、从切分到未切分
3、从切分状态m到切分状态n的转变
比如这里要实现一个通达信的看盘窗口,分成主窗口和指标窗口。指标窗口可随时关闭,也可随时打开。
经过多次尝试,最终确定以下办法
在OnCreateClient中,动态创建目前将要展示的视图,并且保存document指针
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;
}
切换回来的代码与前面如出一辙。
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);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。