CEF3嵌入Win32 工程(2171)

1.准备工作

a.下载 cef_binary_3.2171.1901_windows32.7z 压缩包

b.VS2010 编译器

2. 创建浏览器Win32 工程

a.解压cef_binary_3.2171.1901_windows32.7z 到本地磁盘。

b.打开cefclient2010 如下图


c.可以编译这三个工程,前两个分别是简单浏览器例子 和 一个复杂的浏览器例子,很官方的,但是我们二次开发需要关注libcef_dll_wrapper。


默认情况下:CEF运行主要依赖的库就是libcef_dll_wrapper.lib和libcef.lib,其中libcef_dll_wrapper.lib是静态库(可根据工程需要编译为动态Lib),libcef.lib 动态库。一般编译不会出错,如过工程设置没有问题的话,可能是编译器译器把警告当成错误了,可以在VS关闭这个选项。

d.OK 准备工作完成。搞一个自己的Win32 demo。

新建一个Win项目工程,Cef_2171_win32.sln ,实现最重要的两个类。CCefHandler 和  CCefBrowserApp。

CCefHandler.h 和 CCefHandler.cpp

#pragma once
#include "include/cef_client.h"
#include <list>
#include <string>
using std::wstring;
class CCefHandler:
public CefClient,
public CefDisplayHandler,
public CefLifeSpanHandler,
public CefLoadHandler
{
public:
CCefHandler(void);
~CCefHandler(void);
// Provide access to the single global instance of this object.
static CCefHandler* GetInstance();
// CefClient methods:
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
return this;
}
// CefDisplayHandler methods:
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) OVERRIDE;
// CefLifeSpanHandler methods:
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) OVERRIDE;
// Request that all existing browser windows close.
//void CloseAllBrowsers(bool force_close);
bool IsClosing() const { return is_closing_; }
CefRefPtr<CefBrowser> GetBrowser(){return m_browser;}  
private:
// List of existing browser windows. Only accessed on the CEF UI thread.
//typedef std::list<CefRefPtr<CefBrowser> > BrowserList;
// BrowserList browser_list_;
CefRefPtr<CefBrowser> m_browser; 
bool is_closing_;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(CCefHandler);
};

#include "StdAfx.h"
#include "CefHandler.h"
#include <sstream>
#include "include/cef_app.h"
#include "include/cef_runnable.h"
#include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_helpers.h"
namespace {
CCefHandler* g_instance = NULL;
}  // 
CCefHandler::CCefHandler(void):
is_closing_(false)
{
g_instance = this;
}
CCefHandler::~CCefHandler(void)
{
g_instance = NULL;
}
// static
CCefHandler* CCefHandler::GetInstance() {
return g_instance;
}
void CCefHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
//browser_list_.push_back(browser);
m_browser = browser;
}
bool CCefHandler::DoClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed destription of this
// process.
//if (browser_list_.size() == 1)
if(m_browser)
{
// Set a flag to indicate that the window close should be allowed.
is_closing_ = true;
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void CCefHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title)
{


}
void CCefHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
//BrowserList::iterator bit = browser_list_.begin();
//for (; bit != browser_list_.end(); ++bit) {
//  if ((*bit)->IsSame(browser)) {
//    browser_list_.erase(bit);
//    break;
//  }
//}
//if (browser_list_.empty()) {
//  // All browser windows have closed. Quit the application message loop.
//  CefQuitMessageLoop();
//}
if(m_browser->IsSame(browser))
m_browser = NULL;
}
void CCefHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl)
{
CEF_REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message.
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL " << std::string(failedUrl) <<
" with error " << std::string(errorText) << " (" << errorCode <<
").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}

CCefBrowserApp.h  和 CCefBrowserApp.cpp

#pragma once
 #include "include/cef_app.h"
class CCefBrowserApp:public CefApp,public CefBrowserProcessHandler
{
public:
CCefBrowserApp(void);
~CCefBrowserApp(void);
// CefApp methods:
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
OVERRIDE { return this; }
// CefBrowserProcessHandler methods:
virtual void OnContextInitialized() OVERRIDE;
private:
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(CCefBrowserApp);
};

#include "StdAfx.h"
#include "CefBrowserApp.h"
CCefBrowserApp::CCefBrowserApp(void)
{
}
CCefBrowserApp::~CCefBrowserApp(void)
{
}
void CCefBrowserApp::OnContextInitialized()
{


}

接下来就是初始化CEF ,在新工程的 main 函数入口里初始化。



在窗口过程函数里创建浏览器:

3.让我们新建的工程编译通过 并可以运行

a.建立一个2171lib文件夹 ,建立debug 子文件夹 从编译过的官方demo ,拷贝如下lib

从哪里复制的,你一定能找到的。我不提了哦。

将官方demo 的cef include 目录复制到我们自己的工程目录下。


工程进行相应的配置:


编译的exe(debug)需要这些DLL库:


OK 编译运行 ,大功告成,很简单的demo.



到此一个简单的Win 32 cef 工程就搞成了。先这样吧。有问题请给我留言哦。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值