CEF3:用CEF3实现最简单的浏览器
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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 開發的最簡單的瀏覽器。
總結
以上是生活随笔為你收集整理的CEF3:用CEF3实现最简单的浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: iOS 类似复制链接打开淘宝APP后弹出
- 下一篇: 揭秘ARM FPU 加速浮点计算
