MFC LoadFrame()创建视图、框架参考

1.        BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,

       CWnd* pParentWnd, CCreateContext* pContext)

{

       // only do this once

       ASSERT_VALID_IDR(nIDResource);

       ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

 

       m_nIDHelp = nIDResource;    // ID for help context (+HID_BASE_RESOURCE)

 

       CString strFullString;

       if (strFullString.LoadString(nIDResource))

              AfxExtractSubString(m_strTitle, strFullString, 0);    // first sub-string

 

       VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG));

 

       // attempt to create the window

       LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource);

       LPCTSTR lpszTitle = m_strTitle;

       if (!Create(lpszClass, lpszTitle, dwDefaultStyle, rectDefault,

         pParentWnd, MAKEINTRESOURCE(nIDResource), 0L, pContext))

       {

              return FALSE;   // will self destruct on failure normally

       }

 

       // save the default menu handle

       ASSERT(m_hWnd != NULL);

       m_hMenuDefault = ::GetMenu(m_hWnd);

 

       // load accelerator resource

       LoadAccelTable(MAKEINTRESOURCE(nIDResource));

 

       if (pContext == NULL)   // send initial update

              SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);

 

       return TRUE;

}

 

2.        BOOL CMDIFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,

       CWnd* pParentWnd, CCreateContext* pContext)

{

       if (!CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle,

         pParentWnd, pContext))

              return FALSE;

 

       // save menu to use when no active MDI child window is present

       ASSERT(m_hWnd != NULL);

       m_hMenuDefault = ::GetMenu(m_hWnd);

       if (m_hMenuDefault == NULL)

              TRACE0("Warning: CMDIFrameWnd without a default menu./n");

       return TRUE;

}

 

3.        BOOL CMDIChildWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,

              CWnd* pParentWnd, CCreateContext* pContext)

{

       // only do this once

       ASSERT_VALID_IDR(nIDResource);

       ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

       ASSERT(m_hMenuShared == NULL);      // only do once

 

       m_nIDHelp = nIDResource;    // ID for help context (+HID_BASE_RESOURCE)

 

       // parent must be MDI Frame (or NULL for default)

       ASSERT(pParentWnd == NULL || pParentWnd->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)));

       // will be a child of MDIClient

       ASSERT(!(dwDefaultStyle & WS_POPUP));

       dwDefaultStyle |= WS_CHILD;

 

       // if available - get MDI child menus from doc template

       ASSERT(m_hMenuShared == NULL);      // only do once

       CMultiDocTemplate* pTemplate;

       if (pContext != NULL &&

              (pTemplate = (CMultiDocTemplate*)pContext->m_pNewDocTemplate) != NULL)

       {

              ASSERT_KINDOF(CMultiDocTemplate, pTemplate);

              // get shared menu from doc template

              m_hMenuShared = pTemplate->m_hMenuShared;

              m_hAccelTable = pTemplate->m_hAccelTable;

       }

       else

       {

              TRACE0("Warning: no shared menu/acceltable for MDI Child window./n");

                     // if this happens, programmer must load these manually

       }

 

       CString strFullString, strTitle;

       if (strFullString.LoadString(nIDResource))

              AfxExtractSubString(strTitle, strFullString, 0);    // first sub-string

 

       ASSERT(m_hWnd == NULL);

       if (!Create(GetIconWndClass(dwDefaultStyle, nIDResource),

         strTitle, dwDefaultStyle, rectDefault,

         (CMDIFrameWnd*)pParentWnd, pContext))

       {

              return FALSE;   // will self destruct on failure normally

       }

 

       // it worked !

       return TRUE;

}

 

4.        CMDIChildWnd* CMDIFrameWnd::CreateNewChild(CRuntimeClass* pClass,

              UINT nResources, HMENU hMenu /* = NULL */, HACCEL hAccel /* = NULL */)

{

       ASSERT(pClass != NULL);

       CMDIChildWnd* pFrame = (CMDIChildWnd*) pClass->CreateObject();

       ASSERT_KINDOF(CMDIChildWnd, pFrame);

 

       // load the frame

       CCreateContext context;

       context.m_pCurrentFrame = this;

 

       if (!pFrame->LoadFrame(nResources,

                     WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))

       {

              TRACE0("Couldn't load frame window./n");

              delete pFrame;

              return NULL;

       }

 

       CString strFullString, strTitle;

       if (strFullString.LoadString(nResources))

              AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);

 

       // set the handles and redraw the frame and parent

       pFrame->SetHandles(hMenu, hAccel);

       pFrame->SetTitle(strTitle);

       pFrame->InitialUpdateFrame(NULL, TRUE);

 

       return pFrame;

}

 

5.        void CMDIFrameWnd::OnWindowNew()

{

       CMDIChildWnd* pActiveChild = MDIGetActive();

       CDocument* pDocument;

       if (pActiveChild == NULL ||

         (pDocument = pActiveChild->GetActiveDocument()) == NULL)

       {

              TRACE0("Warning: No active document for WindowNew command./n");

              AfxMessageBox(AFX_IDP_COMMAND_FAILURE);

              return;     // command failed

       }

 

       // otherwise we have a new frame !

       CDocTemplate* pTemplate = pDocument->GetDocTemplate();

       ASSERT_VALID(pTemplate);

       CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, pActiveChild);

       if (pFrame == NULL)

       {

              TRACE0("Warning: failed to create new frame./n");

              return;     // command failed

       }

 

       pTemplate->InitialUpdateFrame(pFrame, pDocument);

}

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值