之前博客《一起学libcef–搭建自己的libcef运行环境(Win32程序,错误C2220解决方案)》讲述了如何在win32程序中搭建libcef的环境,今天就通过一个简单的例子,在windows程序中使用libcef。
现在再重新写一下如何搞?直接在源代码上搞起!
1 打开源码cefclient解决方案
2 确保cefclient例子可以完美运行
3 在cefclient中,除了util.h之外,全部移除
4 manifests 和 resources文件也可以移除(you must remember to remove the additional manifest files from the Visual Studio project file.)
5 libcef_dll_wrapper 是静态链接,所以我们需要更改项目为动态链接。
接下来就要干我们的事儿了:
1 创建一个自己的头文件ExampleCefApp.h, 在这里新建一个类,继承自CefApp:
CefApp 负责所有的工作, 但是这是一个抽象类,需要计数实现
这样, 我们继承自CefApp创建了一个傀儡类,实际上也什么都没做
#include "include/cef_app.h"
class ExampleCefApp : public CefApp
{
public:
ExampleCefApp ()
{
}
virtual ~ExampleCefApp ()
{
}
private:
IMPLEMENT_REFCOUNTING (ExampleCefApp);
};
2创建一个ExampleCefHandler.h文件,这里面实现一个类继承自所有的event handling classes。
#pragma once
#include "include/cef_client.h"
#include "cefclient/util.h"
class ExampleCefHandler : public CefClient,
public CefContextMenuHandler,
public CefDisplayHandler,
public CefDownloadHandler,
public CefDragHandler,
public CefGeolocationHandler,
public CefKeyboardHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRequestHandler
{
public:
ExampleCefHandler();
virtual ~ExampleCefHandler();
CefRefPtr<CefBrowser> GetBrowser();
#pragma region CefClient
// since we are letting the base implementations handle all of the heavy lifting,
// these functions just return the this pointer
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler () OVERRIDE;
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler () OVERRIDE;
virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler () OVERRIDE;
virtual CefRefPtr<CefDragHandler> GetDragHandler () OVERRIDE;
virtual CefRefPtr<CefGeolocationHandler> GetGeolocationHandler () OVERRIDE;
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler () OVERRIDE;
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler () OVERRIDE;
virtual CefRefPtr<CefLoadHandler> GetLoadHandler () OVERRIDE;
virtual CefRefPtr<CefRequestHandler> GetRequestHandler () OVERRIDE;
#pragma endregion // CefClient
#pra