MFC技术内幕系列之(二)---MFC文档视图结构内幕 下

//in afxext.h struct CCreateContext   // Creation information structure // All fields are optional and may be NULL { // for creating new views CRuntimeClass* m_pNewViewClass; // runtime class of view to create or NULL CDocument* m_pCurrentDoc;

// for creating MDI children (CMDIChildWnd::LoadFrame) CDocTemplate* m_pNewDocTemplate;

// for sharing view/frame state from the original view/frame CView* m_pLastView; CFrameWnd* m_pCurrentFrame; // Implementation CCreateContext(); }; 而在CDocTemplate::CreateNewFrame中初始化了该结构如下:       CCreateContext context; context.m_pCurrentFrame = pOther; context.m_pCurrentDoc = pDoc; context.m_pNewViewClass = m_pViewClass; context.m_pNewDocTemplate = this;        context.m_pNewViewClass = m_pViewClass;//关键的成员 下面看看这个创建的具体过程:       LoadFrame(...,&context)-->CFrameWnd::Create(...,&context)--> CWnd::CreateEx(...,&context) -->::CreateWindowEx      ::CreateWindowEx API函数将产生WM_CREATE消息,并将&context传递之,CMainFrame::OnCreate将响应消息,并引起一系列的函数调用,看下面: //in mainfrm.cpp int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)    return -1; ...// return 0; } // in winfrm.cpp int CFrameWnd::OnCreate(LPCREATESTRUCT lpcs) { CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams; return OnCreateHelper(lpcs, pContext); } // in winfrm.cpp int CFrameWnd::OnCreateHelper(LPCREATESTRUCT lpcs, CCreateContext* pContext)//部分源代码 { if (CWnd::OnCreate(lpcs) == -1)    return -1;

// create special children first if (!OnCreateClient(lpcs, pContext)) {    TRACE(traceAppMsg, 0, "Failed to create client pane/view for frame./n");    return -1; }

...//         return 0;   // create ok } // in winfrm.cpp BOOL CFrameWnd::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext) { // default create client will create a view if asked for it if (pContext != NULL && pContext->m_pNewViewClass != NULL) {    if (CreateView(pContext, AFX_IDW_PANE_FIRST) == NULL)     return FALSE; } return TRUE; } // in winfrm.cpp CWnd* CFrameWnd::CreateView(CCreateContext* pContext, UINT nID)//部分源代码 { ...//

// Note: can be a CWnd with PostNcDestroy self cleanup CWnd* pView = (CWnd*)pContext->m_pNewViewClass->CreateObject(); if (pView == NULL) {    TRACE(traceAppMsg, 0, "Warning: Dynamic create of view type %hs failed./n",     pContext->m_pNewViewClass->m_lpszClassName);    return NULL; } ASSERT_KINDOF(CWnd, pView);

// views are always created with a border! if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,    CRect(0,0,0,0), this, nID, pContext)) {    TRACE(traceAppMsg, 0, "Warning: could not create view for frame./n");    return NULL;        // can't continue without a view }

...// return pView; } CWnd* pView = (CWnd*)pContext->m_pNewViewClass->CreateObject();核心函数终于出现了。子视图动态生成完毕。

                       /                        /*         7.收尾工作          */                        /    至此,一套完整的Document/ChildFrame/View结构生成,此“三口组”共属同一套文档模板,如果你要定义另一套不同的文档模档需再定义另一组不同“三口组”(ChildFrame可以使用相同的)。并调用AddDocTemplate将该文档模板加入到应用程序的文档模板列表。比如:

       CMultiDocTemplate* pOtherDocTemplate; pOtherDocTemplate = new CMultiDocTemplate(IDR_MyOtherTYPE,    RUNTIME_CLASS(CMyOtherDoc),    RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架    RUNTIME_CLASS(CMyOtherView)); AddDocTemplate(pOtherDocTemplate);

“三口组”生成后程序调用ShowWindow,UpdateWindow将应用程序的主窗口展现在你眼前。

注释:当你在File菜单中选择new或在工具栏中单击“新建”时,应用程序将选择当前默认的文档模板并以它为基础动态生成 Document/ChildFrame/View“三口组”,其生成过程与我上述讲的一般不二。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值