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的大體流程,目前正在學習中,中間還有一些細節需要慢慢了解。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章