乐趣区

关于mfc:CSplitterWnd动态分割

动态宰割不提了,网上一大堆。要害是动静宰割要怎么办?
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);
 
}
退出移动版