嵌入Chrome cef到MFC CView

公司项目中一直存在着一个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的子类,如下:
[cpp]  view plain copy
  1. #pragma once  
  2. #include <cef_client.h>  
  3.   
  4. class CWebClient   
  5.     : public CefClient  
  6.     , public CefLifeSpanHandler  
  7. {  
  8. protected:  
  9.     CefRefPtr<CefBrowser> m_Browser;  
  10.   
  11. public:  
  12.     CWebClient(void){};  
  13.     virtual ~CWebClient(void){};  
  14.   
  15.     CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }  
  16.   
  17.     virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE  
  18.     { return this; }  
  19.   
  20.     virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;  
  21.   
  22.     // 添加CEF的SP虚函数  
  23.     IMPLEMENT_REFCOUNTING(CWebClient);  
  24.     IMPLEMENT_LOCKING(CWebClient);  
  25. };  

接下来就开始修改我们的视图类了:
创建:
[cpp]  view plain copy
  1. // CWebView message handlers  
  2. int CWebView::OnCreate( LPCREATESTRUCT lpCreateStruct )  
  3. {  
  4.     if ( CView::OnCreate(lpCreateStruct) == -1)  
  5.         return -1;  
  6.   
  7.     CefRefPtr<CWebClient> client(new CWebClient());  
  8.     m_cWebClient = client;  
  9.   
  10.     CefSettings cSettings;  
  11.     CefSettingsTraits::init( &cSettings);  
  12.     cSettings.multi_threaded_message_loop = true;  
  13.     CefRefPtr<CefApp> spApp;  
  14.     CefInitialize( cSettings, spApp);  
  15.       
  16.     CefWindowInfo info;  
  17.     info.SetAsChild( m_hWnd, CRect(0, 0, 800, 600));  
  18.   
  19.     CefBrowserSettings browserSettings;  
  20.     CefBrowser::CreateBrowser( info, static_cast<CefRefPtr<CefClient> >(client),   
  21.         "http://192.168.1.21:8080/dialysis/web/page/nav/content.jsp", browserSettings);  
  22.   
  23.     return 0;  
  24. }  

调整大小:
[cpp]  view plain copy
  1. void CWebView::OnSize( UINT nType, int cx, int cy )  
  2. {  
  3.     CView::OnSize(nType, cx, cy);  
  4.       
  5.     if(m_cWebClient.get())  
  6.     {  
  7.         CefRefPtr<CefBrowser> browser = m_cWebClient->GetBrowser();  
  8.         if(browser)  
  9.         {  
  10.             CefWindowHandle hwnd = browser->GetWindowHandle();  
  11.             RECT rect;  
  12.             this->GetClientRect(&rect);  
  13.             // ::SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, SWP_NOZORDER);  
  14.             ::MoveWindow( hwnd, 0, 0, cx, cy, true);  
  15.         }  
  16.     }  
  17. }  

如果在编译CWebView出现连接错误可以把libcef_dll_wrapper工程的Runtime Library修改为Multi-threaded Debug DLL (/MDd) 并将Treat warnings as errors修改为No (/WX-)试试。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值