嵌入Chrome cef到MFC CView
2014-05-07 11:12
337 查看
转载自:http://www.rzcucc.com/search/blog.csdn.net/yhangleo/article/details/8482603-mfc%20static-2.html
公司项目中一直存在着一个CHtmlView模块来显示URL,但是随着web页面的更新(加入HTML5 and 其它一些比较新的技术)越来越发现使用CHtmlView已经无法满足目前的需求。开始还是试着去修改一些东西去满足当前需要,不过好景不长终于有一天CHtmlView连我们目前的web页面都打不开了,于是决定采用Chrome来作为浏览器引擎。
首先,需要下载CEF框架。其中包含了一个使用CEF的例子。目前的CEF共有3个版本(详情见:http://code.google.com/p/chromiumembedded/downloads/list):
CEF1:单线程的浏览器框架
CEF2:已放弃开发
CEF3:多线程浏览器框架
在这里使用的是CEF1来构建我们的程序。
下载好CEF框架后,打开CEF工程文件找到并编译libcef_dll_wrapper项目(cefclient是一个可运行的例子,有兴趣的朋友可以研究研究)就可以了。
创建好工程后我们需要连接下面这两个静态库:
\cef_binary\Debug\Lib\libcef_dll_wrapper.lib
\cef_binary\lib\Debug\libcef.lib
要与浏览器交互我们需要创建一个CefClient的子类,如下:
[cpp] view
plaincopy
#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);
};
接下来就开始修改我们的视图类了:
创建:
[cpp] view
plaincopy
// CWebView message handlers
int 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;
}
调整大小:
[cpp] view
plaincopy
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-)试试。
公司项目中一直存在着一个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
plaincopy
#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);
};
接下来就开始修改我们的视图类了:
创建:
[cpp] view
plaincopy
// CWebView message handlers
int 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;
}
调整大小:
[cpp] view
plaincopy
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-)试试。
相关文章推荐
- 嵌入Chrome cef到MFC CView
- CEF方面的研究(三) 将CEF嵌入MFC对话框程序
- CEF嵌入到单文档mfc
- 嵌入Chrome cef到MFC CView
- 转:CEF嵌入到单文档mfc
- 嵌入CEF到MFC程序
- MFC嵌入浏览器框架CEF
- 多文档嵌入Chrome CEF求教
- CEF 嵌入到MFC的流程,包括启动CEF以及CEF程序的关闭流程。
- MFC嵌入CEF关闭程序时崩溃的解决方法
- 20150622 CEF嵌入MFC对话框程序
- 嵌入Chrome cef之完整版(本教程基于cef1)
- 将cef3嵌入到mfc对话框中,当程序退出时报错,怎么解决?
- MFC嵌入cef1
- MFC嵌入cef3 WTL嵌入cef3
- MFC嵌入cef3
- 嵌入Chrome cef之完整版(本教程基于cef1)
- MFC应用程序中嵌入一个谷歌cef浏览器
- 201506022 CEF嵌入MFC对话框程序
- MFC对话框程序嵌入CEF并加入flash插件