如何将Chromium Embedded Framework (CEF) 嵌入到你自己的程序中

关于CEF

近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在你自己程序中显示Web。简单的调研后发现,现在很多主流的客户端都使用了CEF来显示Web页面:网易云音乐、QQ、豌豆荚等(安装目录下可以找到libcef.dll)。

下载CEF

我使用的是3.1650.1562版本,CEF的发布版本在这里下载http://www.magpcss.net/cef_downloads/,页面下方的Download List表格中列出的是最新的发布版本(包括Windows、Linux、MacOS平台的32、64位版本)。我下载的是cef_binary_3.1650.1562_windows32,如果表格中的版本不是3.1650.1562,可以点击 older (deprecated) versions去找到。下载到本地之后解压缩到本地文件夹(我解压到了D:\cef_binary_3.1650.1562_windows32),打开文件夹后使用VS2010开打cefclient2010.sln,可以看到两个项目:cefclient、libcef_dll_wrapper。编译Debug版本的cefclient项目,确保可以编译通过,此时会在out目录下(我的目录是D:\cef_binary_3.1650.1562_windows32\out\Debug)生成相应的文件,运行cefclient.exe会显示出Google的页面。cefclient是如何使用cef的一个例子,具体可以参考项目代码。

将CEF嵌入到单文档程序中

在VS2010中,点击菜单项文件-》新建-》项目,选择MFC应用程序,项目名称命名为:CEFBrowser,项目位置根据你上面的解压缩位置而定,我的是D:\cef_binary_3.1650.1562_windows32,解决方案选择“添加到解决方案”,点击“确定”。如下图所示:



在“应用程序类型”中选择“单个文档”,MFC的使用选择“在静态库中使用MFC”,其余的选项默认,点击“完成”。可以看到在解决方案中新添加了CEFBrowser项目。






首先,将CEFBrowser项目设置为启动项目。右键点击CEFBrowser打开项目的“属性页”,点击“通用属性”-》框架和引用-》添加新引用,在弹出的“添加引用”对话框中选择“libcef_dll_wrapper”项目,点击“确定”。如下图所示:




点击“确定”退出属性页。libcef_dll_wrapper生成静态库libcef_dll_wrapper.lib,输出到cef_binary_3.1650.1562_windows32\out\Debug\lib目
录下,我们的程序需要加载这个静态库,当然你也可以在设置中按照目录包含它,或者在程序中使用#pragma comment链接它。

然后,在“配置属性”-》C/C++-》常规-》附加包含目录,加入../,因为我们的程序需要inlclude文件夹下的头文件,include目录位于上一级目录。



其次,将cefclient项目属性页中的链接器-》常规-》附加库目录的内容全部拷贝到CEFBrowser项目的相应位置:



最后,将cefclient属性页的输入-》附加依赖项中的内容全部拷贝到CEFBrowser项目的相应位置,并且将最后的$(Configuration)\libcef.lib前面加上../改为../$(Configuration)\libcef.lib



以上工程的环境就配置好了,下面开始添加使用cef的代码:

  • 给CEFBrowser工程添加一个空白的头文件:ExampleCefApp.h,文件中加入如下代码:
    #pragma once
    #include "include/cef_app.h"
    
    class ExampleCefApp : public CefApp
    {
    public:
    	ExampleCefApp ()
    	{
    	}
    	virtual ~ExampleCefApp ()
    	{
    	}
    
    private:
    	IMPLEMENT_REFCOUNTING (ExampleCefApp);
    };

  • 在给CEFBrowser工程添加一个空白的头文件:ExampleCefHandler.h,文件中加入如下代码:
    #pragma once
    
    #include "include/cef_client.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
    
    #pragma region CefDownloadHandler
    	// this function is virtual and must be implemented; we do nothing in it, so downloading files won't work as the callback function isn't invoked
    	virtual void OnBeforeDownload (CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item, const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback);
    #pragma endregion // CefDownloadHandler 
    
    #pragma region CefLifeSpanHandler
    	// cache a reference to the browser
    	virtual void OnAfterCreated (CefRefPtr<CefBrowser> browser) OVERRIDE;
    	// release the browser reference
    	virtual void OnBeforeClose (CefRefPtr<CefBrowser> browser) OVERRIDE;
    #pragma endregion // CefLifeSpanHandler  
    
    protected:
    	// the browser reference
    	CefRefPtr<CefBrowser> browser;
    
    	// Include the default reference counting implementation.
    	IMPLEMENT_REFCOUNTING (ExampleCefHandler);
    	// Include the default locking implementation.
    	IMPLEMENT_LOCKING (ExampleCefHandler);
    };

  • 然后给CEFBrowser工程添加一个空白的源文件:ExampleCefHandler.cpp,加入代码如下:
    #include "stdafx.h"
    #include "ExampleCefHandler.h"
    
    
    ExampleCefHandler::ExampleCefHandler ()
    {
    }
    
    ExampleCefHandler::~ExampleCefHandler ()
    {
    }
    
    CefRefPtr<CefBrowser> ExampleCefHandler::GetBrowser ()
    {
    	return browser;
    }
    
    CefRefPtr<CefContextMenuHandler> ExampleCefHandler::GetContextMenuHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefDisplayHandler> ExampleCefHandler::GetDisplayHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefDownloadHandler> ExampleCefHandler::GetDownloadHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefDragHandler> ExampleCefHandler::GetDragHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefGeolocationHandler> ExampleCefHandler::GetGeolocationHandler ()
    {
    	return this;
    }
    
    CefRefPtr<CefKeyboardHandler> ExampleCefHandler::GetKeyboardHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefLifeSpanHandler> ExampleCefHandler::GetLifeSpanHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefLoadHandler> ExampleCefHandler::GetLoadHandler () 
    {
    	return this;
    }
    
    CefRefPtr<CefRequestHandler> ExampleCefHandler::GetRequestHandler () 
    {
    	return this;
    }
    
    void ExampleCefHandler::OnBeforeDownload (CefRefPtr<CefBrowser> browser, 
    	CefRefPtr<CefDownloadItem> download_item, 
    	const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback)
    {
    	UNREFERENCED_PARAMETER (browser);
    	UNREFERENCED_PARAMETER (download_item);
    	callback->Continue (suggested_name, true);
    }
    
    void ExampleCefHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) 
    {
    
    	CefLifeSpanHandler::OnAfterCreated (browser);
    }
    
    void ExampleCefHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) 
    {
    	CefLifeSpanHandler::OnBeforeClose (browser);
    }

  • 切换到“类视图”,右键点击CCEFBrowserView-》属性,在属性对话框中给类CCEFBrowserView添加WM_CREATE的响应函数OnCreate。在CEFBrowserView.cpp文件开头处加入如下代码:
    #include "ExampleCefApp.h"
    #include "ExampleCefHandler.h"
    
    #define INVALID_HWND (HWND)INVALID_HANDLE_VALUE
    
    namespace
    {
    	CefRefPtr<ExampleCefHandler> example_cef_handler;
    	HWND application_message_window_handle = INVALID_HWND;
    }

    OnCreate函数的代码如下:
    int CCEFBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CView::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	// TODO:  在此添加您专用的创建代码
    	CefMainArgs main_args ( AfxGetApp()->m_hInstance );
    	CefRefPtr<ExampleCefApp> app (new ExampleCefApp);
    
    	if (CefExecuteProcess(main_args, app.get()) == -1)
    	{
    		CefSettings settings;
    		CefSettingsTraits::init( &settings);
    		settings.multi_threaded_message_loop = true;
    		CefInitialize (main_args, settings, app.get());
    
    		example_cef_handler = new ExampleCefHandler();
    
    		CefWindowInfo info;
    		info.SetAsChild(m_hWnd, CRect(0, 0, 1200, 1200));
    
    		CefBrowserSettings settings1;
    		CefBrowserHost::CreateBrowser(info, example_cef_handler.get(), CefString ("http://www.sina.com"), setting1, NULL);
    	}
    	return 0;
    }


至此,所添加的代码完毕,好了现在编译工程CEFBrowser,在Debug目录

(我的路径是D:\cef_binary_3.1650.1562_windows32\Debug)中确认生成了CEFBrowser.exe,

同时编译器自动的将需要的一些CEF文件也拷贝到了此文件夹下。此时还不能成功运行CEFBrowser.exe

显示出页面,需要将上面我们编译cefclient项目产生的locales文件夹和cef.pak文件

(D:\cef_binary_3.1650.1562_windows32\out\Debug目录中)拷贝到Debug目录下

(提示是否替换,选择是),好了现在可以运行CEFBrowser.exe显示出新浪的页面了(结果如下图)。





猜你喜欢

转载自blog.csdn.net/hideforever/article/details/38435873
CEF
今日推荐