// MFCMdiEx.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
class MFCMyDocument:public CDocument{
DECLARE_DYNCREATE(MFCMyDocument)
public:
CString m_strText;
};
IMPLEMENT_DYNCREATE(MFCMyDocument,CDocument)
class MFCChildView:public CEditView {
DECLARE_DYNCREATE(MFCChildView)
DECLARE_MESSAGE_MAP()
public:
virtual void OnUpdate( CView* pSender, LPARAM lHint, CObject* pHint );
public:
afx_msg void OnEnChang();
};
IMPLEMENT_DYNCREATE(MFCChildView,CEditView)
BEGIN_MESSAGE_MAP(MFCChildView,CEditView)
ON_CONTROL_REFLECT_EX(EN_CHANGE,OnEnChang)
END_MESSAGE_MAP()
//要更新所有的View必须重写此函数
void MFCChildView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint){
//重写了此虚函数 把当前激活的GetDocument获取到他的m_strText
//设置所有View的内容 OnUpdate 中的View链表全部更新从Document拿m_strText更新
//获取View的文档
MFCMyDocument *pDoc=(MFCMyDocument*)GetDocument();
//用此函数修改其他文档的内容
SetWindowText(pDoc->m_strText);
}
void MFCChildView::OnEnChang(){
//首先获取到基础的Document 再获取窗口类的数据 设置到文档里面
//在通知其他窗口更新
//获取View的文字
MFCMyDocument *pDoc=(MFCMyDocument*)GetDocument();
//保存数据到文档
GetWindowText(pDoc->m_strText);
//通知其他View文档数据发生变化
pDoc->UpdateAllViews(this);
}
class MFCChildFrame:public CMDIChildWnd{
DECLARE_DYNCREATE(MFCChildFrame)
public:
};
IMPLEMENT_DYNCREATE(MFCChildFrame,CMDIChildWnd)
class MFCMdiExFrame:public CMDIFrameWnd {
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnCopyWnd();
};
BEGIN_MESSAGE_MAP(MFCMdiExFrame,CMDIFrameWnd)
ON_COMMAND(ID_COPY,OnCopyWnd)
END_MESSAGE_MAP()
void MFCMdiExFrame::OnCopyWnd(){
//获取MDI CHILD Frame窗口
CFrameWnd *pFrameWnd=GetActiveFrame();
if(false ==pFrameWnd->IsKindOf(RUNTIME_CLASS(MFCChildFrame)))
{
return;
}
MFCChildFrame *pChildFrame=( MFCChildFrame* )pFrameWnd;
//获取到VIEW窗口
MFCChildView *pChildView=(MFCChildView*)pChildFrame->GetActiveView();
//获取到文档
MFCMyDocument * pMyDocument=(MFCMyDocument*)pChildView->GetDocument();
//获取文档模板
CWinApp * pWndApp=AfxGetApp();
POSITION pos=pWndApp->m_pDocManager->GetFirstDocTemplatePosition();
CDocTemplate *pDocTemplate=pWndApp->m_pDocManager->GetNextDocTemplate(pos);
MFCChildFrame *pNewFrame=(MFCChildFrame*)
pDocTemplate->CreateNewFrame(pMyDocument,NULL);
//显示窗口
//pNewFrame->ShowWindow(SW_SHOW);
//刷新窗口
pDocTemplate->InitialUpdateFrame(pNewFrame,pMyDocument);
}
class MFCMdiExApp:public CWinApp{
DECLARE_MESSAGE_MAP()
public:
virtual BOOL InitInstance();
public:
afx_msg void OnNew();
};
BEGIN_MESSAGE_MAP(MFCMdiExApp,CWinApp)
ON_COMMAND(ID_NEW,OnNew)
END_MESSAGE_MAP()
void MFCMdiExApp::OnNew(){
OnFileNew();
}
MFCMdiExApp theApp;
BOOL MFCMdiExApp::InitInstance(){
CMultiDocTemplate *pDocTemplate=NULL;
pDocTemplate=new CMultiDocTemplate(
IDR_MAINFRM,
RUNTIME_CLASS(MFCMyDocument),
RUNTIME_CLASS(MFCChildFrame),
RUNTIME_CLASS(MFCChildView));
AddDocTemplate(pDocTemplate);
MFCMdiExFrame *pMainWnd=new MFCMdiExFrame();
pMainWnd->LoadFrame(IDR_MAINFRM);
m_pMainWnd=pMainWnd;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return 1;
}程序下载 http://pan.baidu.com/s/1jGLtn7o