void CMainFrame::OnIMan() { // TODO: Add your command handler code here CRect cr; GetClientRect(&cr); CSize paneSize1(200, 100); CCreateContext Context; Context.m_pNewViewClass=RUNTIME_CLASS(out); Context.m_pCurrentDoc=GetActiveDocument(); Context.m_pCurrentFrame=this; Context.m_pNewDocTemplate=NULL; Context.m_pLastView=(CView*)m_splitter.GetPane(0,0); m_splitter.DeleteView(0, 1); m_splitter.CreateView(0, 1,RUNTIME_CLASS(in),paneSize1, &Context); in *pView=(in*)m_splitter.GetPane(0,1); pView->GetParentFrame()->RecalcLayout(); m_splitter.RecalcLayout(); pView->OnInitialUpdate(); m_splitter.SetActivePane(0,1); } void CMainFrame::OnPan() { // TODO: Add your command handler code here CRect cr; GetClientRect(&cr); CSize paneSize1(200, 100); CCreateContext Context; Context.m_pNewViewClass=RUNTIME_CLASS(in); Context.m_pCurrentDoc=GetActiveDocument(); Context.m_pCurrentFrame=this; Context.m_pNewDocTemplate=NULL; Context.m_pLastView=(CView*)m_splitter.GetPane(0,0); m_splitter.DeleteView(0, 1); m_splitter.CreateView(0, 1,RUNTIME_CLASS(out),paneSize1, &Context); out *pView=(out*)m_splitter.GetPane(0,1); pView->GetParentFrame()->RecalcLayout(); m_splitter.RecalcLayout(); pView->OnInitialUpdate(); m_splitter.SetActivePane(0,1); } 可以看到这里的函数是定义在CMainFrame.cpp文件中的。这里因为上面程序要进行界面控制,所以定义在CMainFrame.cpp文件中方便很多。那么要通过按钮响应这个函数,可以直接调用,代码如下:void mianban::OnIn() { // TODO: Add your control notification handler code here CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd; pFrame->OnMan(); } void mianban::OnOut() { // TODO: Add your control notification handler code here CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd; pFrame->OnPan(); } OK,现在直接通过两个按钮就可以实现两个界面的转换。 前两天写了个数据库操作的小程序。程序里用到了按钮切换界面的功能。为了方便以后查看,现在将功能总结如下:
先来看看效果:
程序中就是通过 DATA Find 和DATA In 来切换两个不同的页面。
界面的划分和固定,在上篇日志已经详细介绍了,这里就不再说了,直接实现切换功能。
首先在OnCreateClient()函数里初始化界面,代码如下:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class m_splitter.CreateStatic(this,1,2); m_splitter.CreateView(0,0,RUNTIME_CLASS(mianban),CSize(250,100),pContext); m_splitter.CreateView(0,1,RUNTIME_CLASS(in),CSize(100,100),pContext); return TRUE; }
然后再mianban界面上添加两个按钮进行切换:
现在就为两个按钮进行响应:代码如下: