#include "stdafx.h" #include "resource.h" //文档类 class CMyDoc:public CDocument { public: CMyDoc() { m_strText="Data From Doc!"; } CString m_strText; }; class CDocView:public CEditView { DECLARE_DYNCREATE(CDocView); public: virtual void OnInitialUpdate(); }; IMPLEMENT_DYNCREATE(CDocView,CEditView) void CDocView::OnInitialUpdate() { CMyDoc* doc= (CMyDoc*)this->GetDocument();//获取视图对应的文档 SetWindowText(doc->m_strText); this->UpdateData(TRUE); } //窗口框架类 class CDocFrame:public CFrameWnd { }; //应用程序类 class CDocApp:public CWinApp { public: virtual BOOL InitInstance(); }; CDocApp theApp; BOOL CDocApp::InitInstance() { CDocFrame *pFrame=new CDocFrame; //动态创建视图 CCreateContext cxt; cxt.m_pCurrentDoc=new CMyDoc; cxt.m_pNewViewClass=RUNTIME_CLASS(CDocView); pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,NULL,&cxt); pFrame->InitialUpdateFrame(NULL,TRUE);//这里才会调用视图的OnInitialUpdate m_pMainWnd=pFrame; pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }
#include "stdafx.h" #include "resource.h" //文档类 class CMyDoc:public CDocument { DECLARE_DYNCREATE(CMyDoc) }; IMPLEMENT_DYNCREATE(CMyDoc,CDocument) //视图类 class CMyView:public CEditView { DECLARE_DYNCREATE(CMyView) }; IMPLEMENT_DYNCREATE(CMyView,CEditView) //应用程序类 class CMyApp:public CWinApp { public: virtual BOOL InitInstance(); }; CMyApp theApp; //框架窗口类 class CMyFrame:public CFrameWnd { DECLARE_DYNCREATE(CMyFrame) }; IMPLEMENT_DYNCREATE(CMyFrame,CFrameWnd) BOOL CMyApp::InitInstance() { //创建单文档模版对象 CSingleDocTemplate *pTemplate=new CSingleDocTemplate(IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc),RUNTIME_CLASS(CMyFrame),RUNTIME_CLASS(CMyView)); //将文档模版对象添加到应用程序 AddDocTemplate(pTemplate); //新建文档 OnFileNew(); //显示更新窗口 m_pMainWnd->ShowWindow(SW_MAXIMIZE); m_pMainWnd->UpdateWindow(); return TRUE; }
文档。最终的目的,一个文档对应多个视图。
多个视图数据同步
1 捕获视图内容发生变化的消息,在消息处理函数中,将当前视图的数据
保存到文档,通知其它视图文档数据发生改变了。
1.1 消息映射,ON_CONTROL_REFLECT
1.2 通知视图数据更新,CDocument::UpdateAllViews
2 其它视图收到通知后,在函数中,将文档的新的数据显示到本视图上
2.1 CView::OnUpdate()函数,视图更新函数,被CDocument::
UpdateAllViews()函数调用。
#include <afxwin.h> #include <afxext.h> #include "stdafx.h" #include "resource.h" //文档类 class CMyDoc:public CDocument { DECLARE_DYNCREATE(CMyDoc) }; IMPLEMENT_DYNCREATE(CMyDoc,CDocument) //视图类 class CMyView:public CEditView { DECLARE_DYNCREATE(CMyView) }; IMPLEMENT_DYNCREATE(CMyView,CEditView) //子框架窗口类-动态创建 class CChildFrame:public CMDIChildWnd { DECLARE_DYNCREATE(CChildFrame) }; IMPLEMENT_DYNCREATE(CChildFrame,CMDIChildWnd) //主框架窗口类 class CMainFrame:public CMDIFrameWnd { DECLARE_MESSAGE_MAP() public: protected: void OnNewView(); }; //新建视图,不新建文档,让一个活动文档对应多个视图 void CMainFrame::OnNewView() { //获取活动子窗口 CChildFrame* pFrame=(CChildFrame*)GetActiveFrame(); //获取活动视图 CMyView*pView=(CMyView*)pFrame->GetActiveView(); //获取活动文档 CMyDoc* pDoc=(CMyDoc*)pView->GetDocument(); //获取文档模版 CDocTemplate*pTemplate=pDoc->GetDocTemplate(); //由模版,基于活动文档 创建新的子窗口 CChildFrame*pNewFrame= (CChildFrame*)pTemplate->CreateNewFrame(pDoc,NULL); pTemplate->InitialUpdateFrame(pNewFrame,NULL); } BEGIN_MESSAGE_MAP(CMainFrame,CMDIFrameWnd) ON_COMMAND(ID_NEW_VIEW,OnNewView) END_MESSAGE_MAP() //应用程序类 class CMyApp:public CWinApp { DECLARE_MESSAGE_MAP() public: virtual BOOL InitInstance(); protected: void OnNew(); }; BEGIN_MESSAGE_MAP(CMyApp,CWinApp) ON_COMMAND(ID_NEW,OnNew) END_MESSAGE_MAP() //新建 void CMyApp::OnNew() { this->OnFileNew(); } BOOL CMyApp::InitInstance() { CMainFrame *pFrame=new CMainFrame; pFrame->LoadFrame(IDR_MAINFRAME); m_pMainWnd=pFrame; pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); CMultiDocTemplate *pTemplate=new CMultiDocTemplate( IDR_CHILDFRAME,RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CChildFrame),RUNTIME_CLASS(CMyView)); //将文档模版对象添加到应用程序 AddDocTemplate(pTemplate); //新建文档 OnFileNew(); return TRUE; } CMyApp theApp;
C++MFC编程笔记day05 文档类-单文档和多文档应用程序,布布扣,bubuko.com
C++MFC编程笔记day05 文档类-单文档和多文档应用程序
原文:http://blog.csdn.net/pukuimin1226/article/details/38292807