MFC多文档窗口创建 窗口内容同步OnUpdate 14.5.6

// 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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值