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

本例开发环境: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 & Destructor
public:
    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)";#1
xcopy G:\libs\cef\lib\Debug\*.dll $(OutDir) /Y /E /F
xcopy G:\libs\cef\lib\Debug\*.bin $(OutDir) /Y /E /F
xcopy G:\libs\cef\Resources\* $(OutDir) /Y /E /F

编译,运行,效果如下:


这里写图片描述

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

  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
CefSharp是一个使用C#编写的基于Google Chromium项目的开源项目,用于在.NET应用程序中集成浏览器功能。它提供了一个简单易用的方式来创建和定制一个功能强大的浏览器应用。 通过使用CefSharp,我们可以轻松地创建一个具备浏览器功能的自定义应用程序。可以将它集成到Windows桌面应用、ASP.NET网页、WPF应用等各种.NET项目中。 使用CefSharp制作浏览器有许多好处。首先,它可以为我们提供一个更强大的浏览器功能,包括支持HTML5、CSS3、JavaScript以及许多其他现代网络技术。这使得我们能够为用户提供一个与主流浏览器相似的体验。 其次,CefSharp具有强大的自定义能力。我们可以根据自己的需求进行修改和定制,包括更改浏览器外观、添加或删除功能等。这为我们提供了很大的灵活性,可以根据不同的项目需求进行个性化的定制。 此外,CefSharp提供了一系列的API和事件,使得我们能够与浏览器进行交互。我们可以通过代码控制浏览器的导航、加载特定的网页内容、执行JavaScript代码等。这让我们能够更好地控制和管理浏览器行为,实现更复杂的功能。 最后,CefSharp是一个活跃的开源项目,拥有一个庞大的社区支持。我们可以从社区中获取到许多有用的资源、文档和代码示例,以便更好地使用和了解CefSharp。 总之,使用CefSharp制作浏览器可以为我们提供强大的浏览器功能、灵活的定制能力、丰富的交互方式以及活跃的社区支持。这使得我们能够创建出功能丰富、用户友好的浏览器应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值