嵌入Chrome cef到MFC CView

來源:互聯網
上載者:User

公司項目中一直存在著一個CHtmlView模組來顯示URL,但是隨著web頁面的更新(加入HTML5 and 其它一些比較新的技術)越來越發現使用CHtmlView已經無法滿足目前的需求。開始還是試著去修改一些東西去滿足當前需要,不過好景不長終於有一天CHtmlView連我們目前的web頁面都打不開了,於是決定採用Chrome來作為瀏覽器引擎。

嵌入到MFC使用CEF首先,需要下載CEF架構。其中包含了一個使用CEF的例子。目前的CEF共有3個版本(詳情見:http://code.google.com/p/chromiumembedded/downloads/list):CEF1:單線程的瀏覽器架構CEF2:已放棄開發CEF3:多線程瀏覽器架構在這裡使用的是CEF1來構建我們的程式。下載好CEF架構後,開啟CEF工程檔案找到並編譯libcef_dll_wrapper項目(cefclient是一個可啟動並執行例子,有興趣的朋友可以研究研究)就可以了。建立我們的CWebView建立好工程後我們需要串連下面這兩個靜態庫:\cef_binary\Debug\Lib\libcef_dll_wrapper.lib\cef_binary\lib\Debug\libcef.lib要與瀏覽器互動我們需要建立一個CefClient的子類,如下:
#pragma once#include <cef_client.h>class CWebClient : public CefClient, public CefLifeSpanHandler{protected:CefRefPtr<CefBrowser> m_Browser;public:CWebClient(void){};virtual ~CWebClient(void){};CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE{ return this; }virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;// 添加CEF的SP虛函數IMPLEMENT_REFCOUNTING(CWebClient);IMPLEMENT_LOCKING(CWebClient);};

接下來就開始修改我們的視圖類了:建立:

// CWebView message handlersint CWebView::OnCreate( LPCREATESTRUCT lpCreateStruct ){ if ( CView::OnCreate(lpCreateStruct) == -1) return -1;CefRefPtr<CWebClient> client(new CWebClient());m_cWebClient = client;CefSettings cSettings;CefSettingsTraits::init( &cSettings);cSettings.multi_threaded_message_loop = true;CefRefPtr<CefApp> spApp;CefInitialize( cSettings, spApp);CefWindowInfo info;info.SetAsChild( m_hWnd, CRect(0, 0, 800, 600));CefBrowserSettings browserSettings;CefBrowser::CreateBrowser( info, static_cast<CefRefPtr<CefClient> >(client), "http://192.168.1.21:8080/dialysis/web/page/nav/content.jsp", browserSettings);return 0;}

調整大小:

void CWebView::OnSize( UINT nType, int cx, int cy ){CView::OnSize(nType, cx, cy);if(m_cWebClient.get()){CefRefPtr<CefBrowser> browser = m_cWebClient->GetBrowser();if(browser){CefWindowHandle hwnd = browser->GetWindowHandle();RECT rect;this->GetClientRect(&rect);// ::SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, SWP_NOZORDER);::MoveWindow( hwnd, 0, 0, cx, cy, true);}}}

如果在編譯CWebView出現串連錯誤可以把libcef_dll_wrapper工程的Runtime Library修改為Multi-threaded Debug DLL (/MDd) 並將Treat warnings as errors修改為No (/WX-)試試。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.