Cef的初始化,使用,及退出

1、初始化CEF

在程序的入口函数App::InitInstance()中进行初始化。

有两种方式:

一种是单实例程序运行,即应用程序只有一个进程

        CefMainArgs main_args(theApp.m_hInstance);
        CefRefPtr<CefApp> app;
        int exit_code = CefExecuteProcess(main_args, app.get(), NULL);
        if (exit_code >= 0) 
        {
            return exit_code;
        }

        CefSettings settings;
        CefSettingsTraits::init(&settings);
        settings.multi_threaded_message_loop = true;
        settings.single_process = true;
        settings.no_sandbox = true;

        CefString(&settings.locale).FromWString(L"UTF-8");
        CefString(&settings.cache_path).FromString("./cachepath/");
        CefInitialize(main_args, settings, app.get(), NULL);

另一种是CEF运行在单独的进程中,此时在进程管理器中会看到有多个应用程序的进程。

        CefSettings settings;
        CefSettingsTraits::init(&settings);
        settings.multi_threaded_message_loop = true;

        CefString(&settings.locale).FromWString(L"UTF-8");

        CefString(&settings.cache_path).FromString("./cachepath/");

        CefMainArgs mainArgs;
        CefRefPtr<CefApp> cefApp;
        CefInitialize(mainArgs, settings, cefApp, NULL);

 

2、创建浏览器

在Dlg::OnInitDialog()中创建

    RECT rect;
    GetClientRect(&rect);
    RECT rectnew = rect;
    rectnew.top = rect.top + m_marginTop;
    rectnew.bottom = rect.bottom - m_marginBottom;
    rectnew.left = rect.left + m_marginLeft;
    rectnew.right = rect.right - m_marginRight;

    CefWindowInfo winInfo;
    winInfo.SetAsChild(GetSafeHwnd(), rectnew);

    m_cefClient = new CCefClient();

    CefBrowserSettings browserSettings;
    CefBrowserHost::CreateBrowser(winInfo, m_cefClient, _T(""), browserSettings, NULL);

 类CCefClient是自字义的一个类,通过这个类对浏览器进行控制,继承Cef的接口类,如:

class CCefClient : public CefClient,
                   public CefLifeSpanHandler,
                   public CefLoadHandler

3、退出

在App::ExitInstance()中调用

CefShutdown();

 

以上是调用CEF的大体流程,目前正在学习中,中间还有一些细节需要慢慢了解。

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值