CEF3:用CEF3實現最簡單的瀏覽器
阿新 • • 發佈:2019-02-08
本例開發環境:WIN10 + VS2015
建立一個空的 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 開發的最簡單的瀏覽器。