Windows uses CEF embedded based on Chrome kernel browser Small example

Source: Internet
Author: User

CEF came out for a long time, the use is also very extensive, QQ inside a lot of places are embedded CEF browser (profile, Weibo, find ...) ), the online information is also quite a lot of, we can search to see.

The first is to download the CEF code compilation, through the inside of the two examples you can also follow the gourd painting scoop. Official: http://cefbuilds.com/

A very detailed explanation is recommended here: http://www.cnblogs.com/think/archive/2011/10/06/CEF-Introduce.html

The various "message" processing classes that overload CEF need to be overloaded when you need to handle them yourself or customize them, and you must override the corresponding virtual function to return the this pointer after overloading. It is estimated that there are these callback class object pointers internally, and that if you do not return this, the default is null, and the internal default set of mechanisms will be executed.

<span style= "Font-family:microsoft yahei;font-size:12px;" > #pragma once#include "include/cef_client.h" #include <list> #include <string>using std::wstring;class Ccefhandler:p ublic cefclient, public cefdisplayhandler, public ceflifespanhandler,public cefloadhandler,public CefR Equesthandler,public cefcontextmenuhandler,public cefdownloadhandler{public:ccefhandler (const wstring& strUrl= L ""); virtual ~ccefhandler ();//Custom Method Cefrefptr<cefbrowser> Getbrowser () {return m_pbrowser;} Cefrefptr<cefframe>getmainfram () {return M_pbrowser.get ()? M_pbrowser->getmainframe (): NULL;} Hwndgetbrowserhostwnd () {return M_pbrowser.get ()? M_pbrowser->gethost ()->getwindowhandle (): NULL; Voidsethomepage (const wstring& strurl) {m_strhomepage=strurl;} Const wstring& Gethomepage () const {return m_strhomepage;} Wstring Getloadingurl (); voidnavigate (const wstring& strurl); Voidcreatebrowser (HWND hparentwnd, const rect& RECT); boolisclosing () const {return m_bisclose; }//All inherited processing functions are here to return the this pointer virtual Cefrefptr<cefdisplayhandler>getdisplayhandler () {return this;} Virtual Cefrefptr<ceflifespanhandler>getlifespanhandler () {return this;} Virtual Cefrefptr<cefloadhandler>getloadhandler () {return this;} Virtual Cefrefptr<cefcontextmenuhandler>getcontextmenuhandler () {return this;}  Virtual Cefrefptr<cefdownloadhandler>getdownloadhandler () {return this;} Cefdisplayhandler methods:virtual void Ontitlechange (cefrefptr<cefbrowser> browser, const cefstring&  title); virtual void Onaddresschange (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, const cefstring  & URL);  virtual bool Ontooltip (cefrefptr<cefbrowser> Browser, cefstring& text);  virtual void Onstatusmessage (cefrefptr<cefbrowser> browser, const cefstring& value); Ceflifespanhandler methods:virtual bool Onbeforepopup (cefrefptr<cefbrowser> browser, cefrefptr<cefframe > Frame, const Cefstring& Target_url, const cefstring& target_frame_name, const cefpopupfeatures& PopupFeatures, cefwindowinfo& Windowinfo, cefrefptr<cefclient>& client, cefbrowsersettings& settings, bool* no_  javascript_access);  virtual void onaftercreated (Cefrefptr<cefbrowser> browser);  virtual bool Doclose (cefrefptr<cefbrowser> browser);  virtual void Onbeforeclose (Cefrefptr<cefbrowser> browser); Cefloadhandler methods:virtual void Onloadstart (cefrefptr<cefbrowser> browser, cefrefptr<cefframe>  frame);  virtual void Onloadend (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, int httpstatuscode); virtual void Onloaderror (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, ErrorCode ErrorCode,  Const cefstring& ErrorText, const cefstring& failedurl);  Request, all existing browser windows close.  void Closeallbrowsers (bool force_close); virtual bool Onbeforebrowse (cefrefptr&Lt Cefbrowser> Browser, cefrefptr<cefframe> frame, cefrefptr<cefrequest> request, bool Is_redirect) {//R  Eturn true;  return false; } virtual bool Onbeforeresourceload (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, cefrefptr& Lt  Cefrequest> request) {return false; }//Menu handling virtual void Onbeforecontextmenu (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, cefref  ptr<cefcontextmenuparams> params, cefrefptr<cefmenumodel> model); virtual bool Oncontextmenucommand (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, cefrefptr  <CefContextMenuParams> params, int command_id, eventflags event_flags); Download processing virtual void Onbeforedownload (cefrefptr<cefbrowser> browser, cefrefptr<cefdownloaditem> Download_  Item, const cefstring& Suggested_name, cefrefptr<cefbeforedownloadcallback> callback); virtual void ondownloadupdated (cefrefptr<cefbrowser> browser, CEfrefptr<cefdownloaditem> Download_item, cefrefptr<cefdownloaditemcallback> callback);p Rivate:typedef std::list<cefrefptr<cefbrowser> > Browserlist; Browserlist Browser_list_; Cefrefptr<cefbrowser>m_pbrowser;boolm_bisclose;wstringm_strhomepage;static intm_nBrowserCount;//Include The default reference counting implementation. Implement_refcounting (Ccefhandler); implement_locking (Ccefhandler);}; </span>

The corresponding implementation code:

<span style= "Font-family:microsoft yahei;font-size:12px;" > #include "stdafx.h" #include "CefHandler.h" #include <sstream> #include "util.h" #include ". /include/cef_app.h "#include". /include/cef_runnable.h "Intccefhandler::m_nbrowsercount = 0; Ccefhandler::ccefhandler (const wstring& strurl/*=l "" */): M_bisclose (False), M_strhomepage (strURL) {}CCefHandler :: ~ccefhandler () {}void ccefhandler::closeallbrowsers (bool force_close) {if (! Cefcurrentlyon (TID_UI)) {//Execute on the UI thread.  Cefposttask (TID_UI, Newcefrunnablemethod (this, &ccefhandler::closeallbrowsers, Force_close)); return;}  if (Browser_list_.empty ()) return;  Browserlist::const_iterator it = Browser_list_.begin (); for (; it! = Browser_list_.end (); ++it) (*it)->gethost ()->closebrowser (force_close);} Wstring Ccefhandler::getloadingurl () {cefrefptr<cefframe> Pmainfram=getmainfram (); return Pmainfram.get ()? Pmainfram->geturl (): L "";} void Ccefhandler::navigate (const wstring& strurl) {CEFREFPTR&LT Cefframe> Pmainfram=getmainfram (); if (Pmainfram.get ()) Pmainfram->loadurl (Strurl.c_str ());} void Ccefhandler::createbrowser (HWND hparentwnd, const rect& RECT) {cefwindowinfo info; Cefbrowsersettings settings;static wchar_t* pcharset = L "GB2312"; settings.default_encoding.str = Pcharset; Settings.default_encoding.length = Wcslen (pcharset); info. Setaschild (Hparentwnd, rect); Cefbrowserhost::createbrowser (info, this, m_strhomepage.c_str (), settings, NULL);} Menu load interface void Ccefhandler::onbeforecontextmenu (cefrefptr <CefBrowser> Browser, cefrefptr<cefframe> frame, cefrefptr<cefcontextmenuparams> params, cefrefptr <CefMenuModel> model) {//Here I have added the menu I want cef_context_menu_type_flags_t flag = params->gettypeflags (); if ( Flag & Cm_typeflag_page) {//Normal page right-click Message Model->setlabel (Menu_id_back, L "back"); Model->setlabel (menu_id_ FORWARD, L "forward"); Model->setlabel (Menu_id_view_source, l "View source code"); Model->setlabEl (Menu_id_print, L "print"); Model->setlabel (Menu_id_reload, L "Refresh"); Model->setlabel (Menu_id_reload_nocache, L " Forced refresh "), Model->setlabel (Menu_id_stopload, L" Stop loading "), Model->setlabel (Menu_id_redo, L" repeat ");} if (flag & cm_typeflag_editable) {//edit box right-click Message Model->setlabel (Menu_id_undo, L "undo"); Model->setlabel (menu_id_ REDO, L "Redo"), Model->setlabel (Menu_id_cut, L "cut"), Model->setlabel (menu_id_copy, L "copy"), Model->setlabel ( Menu_id_paste, L "paste"), Model->setlabel (Menu_id_delete, L "delete"), Model->setlabel (Menu_id_select_all, L "Select All");}}    BOOL Ccefhandler::oncontextmenucommand (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, cefrefptr<cefcontextmenuparams> params, int command_id, eventflags event_flags) {return false;} Page load status callback interface void Ccefhandler::onloadstart (cefrefptr< Cefbrowser> Browser, cefrefptr<cefframe> frame) {}void Ccefhandler::onloadend (cefrefptr<cefbrowser> Browser, CEFREFPtr<cefframe> frame, int httpstatuscode) {//cefrefptr<cefv8context> v8 = Frame->getv8context ();} void Ccefhandler::onloaderror (cefrefptr<cefbrowser> browser, cefrefptr<cefframe> frame, ErrorCode  ErrorCode, const cefstring& errorText, const cefstring& failedurl) {require_ui_thread ();//Don ' t display an error For downloaded files.if (errorCode = = err_aborted) return;//Display a load error message.std::wstringstream SS;//STD::WST Ring SS << L "
The overloads of the Cefapp class are then implemented, without the actual processing functionality, which is ignored here.

When used, create a Win32 project and then modify the WinMain function:

<span style= "Font-family:microsoft yahei;font-size:12px;" >CefRefPtr<CCefHandler> g_handler;int apientry _tWinMain (hinstance hinstance, hinstance Hprev Instance, LPTSTR lpcmdline, int ncmdshow) {//TODO: Place code here. Cefmainargs Main_args (HINSTANCE); Cefrefptr<ccefappex> app (new Ccefappex); int exit_code = Cefexecuteprocess (Main_args, App.get ()); if (Exit_code >= 0) {return exit_code;} Cefsettings settings;settings.single_process = True;//settings.multi_threaded_message_loop = true; Cefinitialize (Main_args, Settings, App.get ()),//if (strcmd.size () = = 0) haccel hacceltable;//Initialize global string loadstring ( HINSTANCE, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_cefdemo, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization: if (! InitInstance (HINSTANCE, nCmdShow)) {return FALSE;} hacceltable = Loadaccelerators (hinstance, Makeintresource (Idc_cefdemo)); Cefrunmessageloop (); Cefshutdown (); RETUrn 0;} </span>
message processing mainly is to create, destroy CEF objects, the window size changes when the CEF window is notified, etc.:

<span style= "Font-family:microsoft yahei;font-size:12px;" >lresult CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent; Paintstruct PS; HDC hdc;switch (message) {case Wm_create:{//http://blog.csdn.net/wongson/article/details/6210854g_handler=new Ccefhandler (L "www.qq.com"); Rect Rect;::getclientrect (hwnd, &rect); G_handler->createbrowser (hwnd, RECT); break;} Case wm_command:wmid = LoWord (wParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {case Idm_exit:destroywindow (hwn d); break;default:break;} Break;case WM_PAINT:HDC = BeginPaint (hWnd, &ps);//TODO: Add any drawing code here ... EndPaint (HWnd, &ps); Break;case wm_size:{if (WParam = size_minimized | | NULL = = g_handler| | NULL = = G_handler->getbrowserhostwnd ()) break; HWND Hbrowserwnd=g_handler->getbrowserhostwnd (); RECT Rect;::getclientrect (hWnd, &rect);:: MoveWindow (Hbrowserwnd, Rect.left, Rect.top, Rect.right-rect.left, Rect.bottom-rect.top, TRUE); break;} Case Wm_close:{if (G_handler.get() &&!g_handler->isclosing ()) {wstring strurl=g_handler->getloadingurl (); Cefrefptr<cefbrowser> Pbrowser=g_handler->getbrowser (); if (Pbrowser.get ()) Pbrowser->gethost () CloseBrowser (TRUE);} break;} Default:break;} Return DefWindowProc (hWnd, message, WParam, LParam);} </span>
compile, run the program, a simple page came out, loading speed than ie that garbage much faster, the key is to ignore the compatibility issue.



Finally remember to bring a lot of CEF DLL, this is necessary, if the webpage needs to play video, need to create a new plugins folder, put into the video playback plugin NPSWF32.dll.

CEF dynamic link libraries that need to be linked:

<span style= "Font-family:microsoft yahei;font-size:12px;" > #ifdef _debug#pragma Comment (lib, "... \\LibCef\\Debug\\libcef ") #pragma comment (lib,". \\LibCef\\Debug\\libcef_dll_wrapper ") #else #pragma comment (lib," ... \\LibCef\\Release\\libcef ") #pragma comment (lib,". \\LibCef\\Release\\libcef_dll_wrapper ") #endif </span>


Windows uses CEF embedded based on Chrome kernel browser Small example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.