CEF3:用CEF3实现最简单的浏览器

来源:互联网 发布:zookeeper mysql 实战 编辑:程序博客网 时间:2024/06/05 07:40

本例开发环境:WIN10 + VS2015

如果还没有编译CEF3库,请见:Windows下用VS2015编译CEF3

创建一个空的 Windows 应用程序,命名为 SimpleBrowser,如下图:


这里写图片描述

新建 main.cpp ,编写如下代码:

#include "include/cef_app.h"#include "include/cef_browser.h"#include "include/cef_client.h"#include "include/wrapper/cef_closure_task.h"#include "include/wrapper/cef_helpers.h"#include <Windows.h>class MyClient : public CefClient, public CefLifeSpanHandler{    // Constructor & Destructorpublic:    virtual ~MyClient() {}    // CefClient methods:public:    virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override    {        return this;    }    // CefLifeSpanHandler methods:public:    virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override    {        CefQuitMessageLoop();    }private:    // Include the default reference counting implementation.    IMPLEMENT_REFCOUNTING(MyClient);};// Implement application-level callbacks for the browser process.class MyApp : public CefApp, public CefBrowserProcessHandler{public:    virtual ~MyApp() {}    // CefApp methods:    virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override { return this; }    // CefBrowserProcessHandler methods:    virtual void OnContextInitialized() override    {        CEF_REQUIRE_UI_THREAD();        // Information used when creating the native window.        CefWindowInfo window_info;        // SimpleHandler implements browser-level callbacks.        CefRefPtr<MyClient> client(new MyClient());        // On Windows we need to specify certain flags that will be passed to        // CreateWindowEx().        window_info.SetAsPopup(NULL, "cefsimple");        // Specify CEF browser settings here.        CefBrowserSettings browser_settings;        // Create the first browser window.        CefString url = "http://www.baidu.com";        CefBrowserHost::CreateBrowser(window_info, client, url, browser_settings, NULL);    }private:    // Include the default reference counting implementation.    IMPLEMENT_REFCOUNTING(MyApp);};int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){    // Provide CEF with command-line arguments.    CefMainArgs main_args(hInstance);    // CEF applications have multiple sub-processes (render, plugin, GPU, etc)    // that share the same executable. This function checks the command-line and,    // if this is a sub-process, executes the appropriate logic.    int exit_code = CefExecuteProcess(main_args, NULL, NULL);    if (exit_code >= 0)    {        // The sub-process has completed so return here.        return exit_code;    }    // Specify CEF global settings here.    CefSettings settings;    settings.no_sandbox = true;    // SimpleApp implements application-level callbacks for the browser process.    // It will create the first browser instance in OnContextInitialized() after    // CEF has initialized.    auto myApp = CefRefPtr<MyApp>(new MyApp());    // Initialize CEF.    CefInitialize(main_args, settings, myApp.get(), NULL);    // Run the CEF message loop. This will block until CefQuitMessageLoop() is    // called.    CefRunMessageLoop();    // Shut down CEF.    CefShutdown();    return 0;}

[工程属性] -> [C/C++] ,将 cef 库的 include 所在目录添加到 [附加包含目录]:


这里写图片描述

[工程属性] -> [链接器],设置好 [附加库目录] 和 [附加依赖项]:

这里写图片描述

[工程属性] -> [后期生成事件],在命令行里输入如下内容,将依赖的二进制和资源拷贝过来。另外注意需要将 manifest 清单文件嵌入到最后生成的 exe 中,否则可能无法正常运行。

mt.exe -nologo -manifest "G:\libs\cef\manifest\cef.exe.manifest" "G:\libs\cef\manifest\compatibility.manifest" -outputresource:"$(OutDir)$(TargetFileName)";#1xcopy G:\libs\cef\lib\Debug\*.dll $(OutDir) /Y /E /Fxcopy G:\libs\cef\lib\Debug\*.bin $(OutDir) /Y /E /Fxcopy G:\libs\cef\Resources\* $(OutDir) /Y /E /F

编译,运行,效果如下:


这里写图片描述

以上就是用 CEF3 开发的最简单的浏览器。

原创粉丝点击