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
    评论
以下是在MFC多文档视图中集成CEF3的详细过程: 1. 下载CEF3库并添加到MFC应用程序中。 从CEF3官方网站上下载最新的CEF3库,并将它们添加到您的MFC应用程序中。将CEF3库添加到您的MFC应用程序中的方法与添加其他库的方法相同。 2. 创建一个新的视图类并继承自CefClient类。 在您的应用程序中创建一个新的视图类,并将其继承自CefClient类。这将使您的应用程序能够使用CEF3。 ``` class CCefView : public CView, public CefClient { public: CCefView(); ~CCefView(); CefRefPtr<CefBrowser> GetBrowser() const { return m_Browser; } protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView); virtual void OnSize(UINT nType, int cx, int cy); virtual void OnDestroy(); CefRefPtr<CefBrowser> m_Browser; // CefClient methods virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler(); virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler(); virtual CefRefPtr<CefLoadHandler> GetLoadHandler(); virtual CefRefPtr<CefRequestHandler> GetRequestHandler(); virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser); virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode); virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId sourceProcess, CefRefPtr<CefProcessMessage> message); }; ``` 3. 创建一个新的文档类并继承自CMultiDocTemplate类。 在您的应用程序中创建一个新的文档类,并将其继承自CMultiDocTemplate类。这将使您的应用程序能够支持多文档视图。 ``` class CCefDoc : public CDocument { public: CCefDoc(); DECLARE_DYNCREATE(CCefDoc) protected: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); DECLARE_MESSAGE_MAP() }; ``` ``` IMPLEMENT_DYNCREATE(CCefDoc, CDocument) BEGIN_MESSAGE_MAP(CCefDoc, CDocument) END_MESSAGE_MAP() CCefDoc::CCefDoc() { } BOOL CCefDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; return TRUE; } void CCefDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { } else { } } ``` 4. 创建一个新的窗口类并继承自CFrameWnd类。 在您的应用程序中创建一个新的窗口类,并将其继承自CFrameWnd类。这将是您的应用程序的主窗口。 ``` class CCefFrameWnd : public CFrameWnd { public: CCefFrameWnd(); protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); DECLARE_MESSAGE_MAP() }; ``` ``` IMPLEMENT_DYNCREATE(CCefFrameWnd, CFrameWnd) BEGIN_MESSAGE_MAP(CCefFrameWnd, CFrameWnd) END_MESSAGE_MAP() CCefFrameWnd::CCefFrameWnd() { } BOOL CCefFrameWnd::PreCreateWindow(CREATESTRUCT& cs) { if (!CFrameWnd::PreCreateWindow(cs)) return FALSE; cs.style |= WS_MAXIMIZE; return TRUE; } ``` 5. 创建一个新的视图类并继承自CView类。 在您的应用程序中创建一个新的视图类,并将其继承自CView类。这将是您的应用程序的主视图。 ``` class CCefMainFrame : public CView { public: CCefMainFrame(); protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); DECLARE_MESSAGE_MAP() }; ``` ``` IMPLEMENT_DYNCREATE(CCefMainFrame, CView) BEGIN_MESSAGE_MAP(CCefMainFrame, CView) END_MESSAGE_MAP() CCefMainFrame::CCefMainFrame() { } BOOL CCefMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if (!CView::PreCreateWindow(cs)) return FALSE; cs.style |= WS_MAXIMIZE; return TRUE; } ``` 6. 实现CEF3的初始化和销毁方法。 在您的应用程序中实现CEF3的初始化和销毁方法。在您的应用程序的InitInstance()方法中初始化CEF3,在您的应用程序的ExitInstance()方法中销毁CEF3。 ``` #include "include/cef_app.h" #include "include/cef_client.h" class CCefApp : public CWinApp, public CefApp { public: CCefApp(); // CefApp methods virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler(); virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler(); protected: virtual BOOL InitInstance(); virtual int ExitInstance(); CefMainArgs m_MainArgs; CefRefPtr<CefBrowserProcessHandler> m_BrowserProcessHandler; CefRefPtr<CefRenderProcessHandler> m_RenderProcessHandler; DECLARE_MESSAGE_MAP() }; CCefApp::CCefApp() { } CefRefPtr<CefBrowserProcessHandler> CCefApp::GetBrowserProcessHandler() { return m_BrowserProcessHandler; } CefRefPtr<CefRenderProcessHandler> CCefApp::GetRenderProcessHandler() { return m_RenderProcessHandler; } BOOL CCefApp::InitInstance() { CefSettings settings; CefInitialize(m_MainArgs, settings, this); m_BrowserProcessHandler = new CCefBrowserProcessHandler(); m_RenderProcessHandler = new CCefRenderProcessHandler(); return TRUE; } int CCefApp::ExitInstance() { CefShutdown(); return CWinApp::ExitInstance(); } CCefApp theApp; ``` 7. 实现CEF3的消息循环。 在您的应用程序中实现CEF3的消息循环。您需要重写您的应用程序的消息循环,以便您的应用程序能够处理CEF3的消息。 ``` BOOL CCefApp::PumpMessage() { CefDoMessageLoopWork(); return CWinApp::PumpMessage(); } BOOL CCefApp::OnIdle(LONG lCount) { CefDoMessageLoopWork(); return CWinApp::OnIdle(lCount); } ``` 8. 实现CEF3的事件处理方法。 在您的应用程序中实现CEF3的事件处理方法。您需要重写CefClient类的虚拟方法,以便您的应用程序能够处理CEF3的事件。 ``` CefRefPtr<CefBrowserProcessHandler> CCefView::GetBrowserProcessHandler() { return ((CCefApp*)AfxGetApp())->GetBrowserProcessHandler(); } CefRefPtr<CefRenderProcessHandler> CCefView::GetRenderProcessHandler() { return ((CCefApp*)AfxGetApp())->GetRenderProcessHandler(); } CefRefPtr<CefLoadHandler> CCefView::GetLoadHandler() { return this; } CefRefPtr<CefRequestHandler> CCefView::GetRequestHandler() { return this; } void CCefView::OnAfterCreated(CefRefPtr<CefBrowser> browser) { m_Browser = browser; CRect rect; GetClientRect(rect); browser->GetHost()->WasResized(); browser->GetHost()->Invalidate(PET_VIEW); browser->GetHost()->SendFocusEvent(true); } void CCefView::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) { if (browser->IsPopup()) return; if (frame->IsMain()) { browser->GetHost()->SetZoomLevel(0.0); browser->GetHost()->SendFocusEvent(true); } } bool CCefView::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId sourceProcess, CefRefPtr<CefProcessMessage> message) { return false; } ``` 9. 实现多文档视图的事件处理方法。 在您的应用程序中实现多文档视图的事件处理方法。您需要重写您的应用程序的多文档视图事件处理方法,以便您的应用程序能够处理多文档视图的事件。 ``` void CMainFrame::OnCreateNewView() { CCefDoc* pDoc = (CCefDoc*)GetActiveDocument(); if (!pDoc) return; CCefView* pView = new CCefView(); if (!pView) return; CRect rect; GetClientRect(rect); if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, rect, this, AFX_IDW_PANE_FIRST, NULL)) { delete pView; return; } pView->OnActivateView(TRUE, pView, pView); pDoc->AddView(pView); pView->SetFocus(); } void CMainFrame::OnSize(UINT nType, int cx, int cy) { CFrameWnd::OnSize(nType, cx, cy); CWnd* pWnd = GetDescendantWindow(AFX_IDW_PANE_FIRST); if (pWnd) pWnd->SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER); } ``` 10. 实现主窗口和主视图的事件处理方法。 在您的应用程序中实现主窗口和主视图的事件处理方法。您需要重写您的应用程序的主窗口和主视图事件处理方法,以便您的应用程序能够处理主窗口和主视图的事件。 ``` BOOL CCefApp::InitInstance() { CefEnableHighDPISupport(); CWinApp::InitInstance(); AfxEnableControlContainer(); CCefFrameWnd* pFrame = new CCefFrameWnd(); if (!pFrame) return FALSE; if (!pFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pFrame; m_pMainWnd->SetWindowText(_T("MFC CEF3 Demo")); CCefMainFrame* pView = new CCefMainFrame(); if (!pView) return FALSE; if (!pView->Create(NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), m_pMainWnd, AFX_IDW_PANE_FIRST, NULL)) return FALSE; CCefDoc* pDoc = new CCefDoc(); if (!pDoc) return FALSE; pDoc->AddView(pView); pFrame->InitialUpdateFrame(pDoc, TRUE); return TRUE; } int CCefApp::ExitInstance() { CefShutdown(); return CWinApp::ExitInstance(); } ``` 以上是在MFC多文档视图中集成CEF3的详细过程。您可以根据您的应用程序需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值