原创  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);

}

 

发表于 @ 2007年11月08日 12:48:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:MFC/VC++操作Office必需的类型库参考 | 新一篇:MFC/VC++调用word进行报表制作

  • 发表评论
  • 评论内容:
  •  
Copyright © teleinfor
Powered by CSDN Blog