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的大体流程,目前正在学习中,中间还有一些细节需要慢慢了解。