Adding multiple views to an SDI application
1.build an normal SDI project
2.Add a new view class derived from CView
3.Add debug and non debug versions of the GetDocument() function
4.Add a public constructor
5.make the destructor provided by class wizard public
6.include the document class header file in the source file of the new view class
7.add a handler for the OnDraw event of the new view class
8include the header file of the new view class in MainFrm.cpp
9add two protected variables in the CMainFrame class
These variables will be pointers to the two view objects your application uses. Initialize these
pointers to NULL in the constructor of CMainFrame,as shown in the following
class CMainFrame:public CFrameWnd
{
...
protected:
CItalicsView* m_pItalicView;
CDefaultView* m_pDefaultView;
...
}
CMainFrame::CMainFrame()
{
m_pItalicsView=NULL;
m_pDefaultView=NULL;
}
10.add two menu selection to permit the application to switch between the two views.
11.add command handlers to CMainFrame for the two new menu items.
void CMainFrame::OnViewItalics()
{ CDocument* pDoc=GetActiveDocument();
if(NULL==m_pItalicView)
{
m_pDefaultView=(CDefaultView*)GetActiveView();
m_pItalicsView=new CItalicsView(NULL);
m_pItalicsView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rectDefault,this,AFX_IDW_PANE_FIRST+1);
} // m_pDefaultView->SaveData(); 保存另一个视图 pDoc->AddView(m_pItalicsView);
m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);
m_pItalicsView->ShowWindow(SW_SHOW);
m_pDefaultView->ShowWindow(SW_HIDE);
SetActiveView(m_pItalicsView);
pDoc->RemoveView(m_pDefaultView);
RecalcLayout();
}
void CMainFrame::OnViewDefault()
{
// m_pItalicsView->SaveData(); 保存另一个视图 CDocument* pDoc=GetActiveDocument();
pDoc->AddView(m_pDefaultView);
m_pItalicsView->ShowWindow(SW_HIDE);
m_pDefaultView->ShowWindow(SW_SHOW);
m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);
m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
SetActiveView(m_pDefaultView);
pDoc->RemoveView(m_pItalicsView);
RecalcLayout();
} OnViewItalics和OnViewDefault是切换视图的两个方法,你完全可以在两个视图类里面分别添加一个SaveData的方法,然后在切换时调用。
1.build an normal SDI project
2.Add a new view class derived from CView
3.Add debug and non debug versions of the GetDocument() function
4.Add a public constructor
5.make the destructor provided by class wizard public
6.include the document class header file in the source file of the new view class
7.add a handler for the OnDraw event of the new view class
8include the header file of the new view class in MainFrm.cpp
9add two protected variables in the CMainFrame class
These variables will be pointers to the two view objects your application uses. Initialize these
pointers to NULL in the constructor of CMainFrame,as shown in the following
class CMainFrame:public CFrameWnd
{
...
protected:
CItalicsView* m_pItalicView;
CDefaultView* m_pDefaultView;
...
}
CMainFrame::CMainFrame()
{
m_pItalicsView=NULL;
m_pDefaultView=NULL;
}
10.add two menu selection to permit the application to switch between the two views.
11.add command handlers to CMainFrame for the two new menu items.
void CMainFrame::OnViewItalics()
{ CDocument* pDoc=GetActiveDocument();
if(NULL==m_pItalicView)
{
m_pDefaultView=(CDefaultView*)GetActiveView();
m_pItalicsView=new CItalicsView(NULL);
m_pItalicsView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,rectDefault,this,AFX_IDW_PANE_FIRST+1);
} // m_pDefaultView->SaveData(); 保存另一个视图 pDoc->AddView(m_pItalicsView);
m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);
m_pItalicsView->ShowWindow(SW_SHOW);
m_pDefaultView->ShowWindow(SW_HIDE);
SetActiveView(m_pItalicsView);
pDoc->RemoveView(m_pDefaultView);
RecalcLayout();
}
void CMainFrame::OnViewDefault()
{
// m_pItalicsView->SaveData(); 保存另一个视图 CDocument* pDoc=GetActiveDocument();
pDoc->AddView(m_pDefaultView);
m_pItalicsView->ShowWindow(SW_HIDE);
m_pDefaultView->ShowWindow(SW_SHOW);
m_pItalicsView->SetDlgCtrlID(AFX_IDW_PANE_FIRST+1);
m_pDefaultView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
SetActiveView(m_pDefaultView);
pDoc->RemoveView(m_pItalicsView);
RecalcLayout();
} OnViewItalics和OnViewDefault是切换视图的两个方法,你完全可以在两个视图类里面分别添加一个SaveData的方法,然后在切换时调用。
解决方案 »
- 求去回贴,要结贴。
- 不明白htonl的原理?
- 麻烦大家推荐基本vc的实例的书。。
- 日期计算问题:怎样计算SYSTEMTIME的值加减n秒
- 在win32 console程序中能调用dos的图形中断吗?
- 一副真彩色的图片,调整显示器色深, 显示出来可能是16位的, 那它是通过什么规则转换的.
- 简单的问题(请教拉)
- 请问 UP 什么意思?不要笑我,我是诚实的好孩子。另外,可否介绍关于 PE方面的书。
- 我是个新手,虽然这个问题被问过很多遍了,但我恳请各位再给我一次机会
- 一个关于限制迅雷等P2P软件抢网速的老话题。想自己编程实现的哦,有经验的请进
- 全局变量的声明
- 小弟一点编程基础没有,想学好vc+,求大虾提供几本好的入门书、实例书(很重要)、还有比较全的宝典类参考书,谢谢!
{
m_nView=nView;
CView* pOldActiveView=GetActiveView();
CView* pNewActiveView=(CView*)GetDlgItem(nView);
if(pNewActiveView==NULL)
{
switch(nView)
{
case 1:
pNewActiveView=(CView*) new CSTest2View;
break;
case 2:
pNewActiveView=(CView*) new CFormView1;
break;
}
CCreateContext context;
context.m_pCurrentDoc=pOldActiveView->GetDocument();
pNewActiveView->Create(NULL,NULL,WS_BORDER|WS_CHILD,CFrameWnd::rectDefault,this,nView,&context);
pNewActiveView->OnInitialUpdate();
}
SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
if(pOldActiveView->GetRuntimeClass()==RUNTIME_CLASS(CSTest2View))
pOldActiveView->SetDlgCtrlID(1);
else if(pOldActiveView->GetRuntimeClass()==RUNTIME_CLASS(CFormView1))
pOldActiveView->SetDlgCtrlID(2);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
delete pOldActiveView;
RecalcLayout();
}
我是用这种方法切换的, 你说的那不知道能继承CFormView类不?我的这种方法要怎么保存变量的值呢?3q