Windows上使用CEF嵌入基于chrome内核浏览器小例

Windows上使用CEF嵌入基于chrome内核浏览器小例浏览器代码已开源 欢迎收藏 https github com JelinYao MyChromeCEF 出来很久了 使用的也很广泛的 里面很多地方都是嵌入的 CEF 浏览器 个人资料 微博 查找 网上的资料也挺多的 大家可以搜搜看 首先是下载 CEF 代码编译 通过里面的那两个例子你也可以依葫芦画瓢的 官方下载地址 http cefbuilds com 这里推荐一个很详细的解说

浏览器代码已开源:欢迎收藏 https://github.com/JelinYao/MyChrome

CEF出来很久了,使用的也很广泛的,里面很多地方都是嵌入的CEF浏览器(个人资料、微博、查找……),网上的资料也挺多的,大家可以搜搜看。

首先是下载CEF代码编译,通过里面的那两个例子你也可以依葫芦画瓢的。官方下载地址:http://cefbuilds.com/

这里推荐一个很详细的解说:http://www.cnblogs.com/think/archive/2011/10/06/CEF-Introduce.html

重载CEF的各种“消息”处理类,当你需要自己处理或者自定义这些消息时,才需要重载它们,重载后一定要重写相应的虚函数返回this指针。估计内部是有这些个回调类对象指针,不返回this的话默认就是NULL了,就会执行内部默认的那套机制。

 

#pragma once #include "include/cef_client.h" #include 
  
    #include 
   
     using std::wstring; class CCefHandler : public CefClient, public CefDisplayHandler, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler, public CefContextMenuHandler, public CefDownloadHandler { public: CCefHandler(const wstring& strUrl=L""); virtual ~CCefHandler(); //自定义方法 CefRefPtr 
    
      GetBrowser() { return m_pBrowser; } CefRefPtr 
     
       GetMainFram() { return m_pBrowser.get()?m_pBrowser->GetMainFrame():NULL; } HWND GetBrowserHostWnd() { return m_pBrowser.get()?m_pBrowser->GetHost()->GetWindowHandle():NULL; } void SetHomePage(const wstring& strUrl) { m_strHomePage=strUrl; } const wstring& GetHomePage()const { return m_strHomePage; } wstring GetLoadingUrl(); void Navigate(const wstring& strUrl); void CreateBrowser(HWND hParentWnd, const RECT& rect); bool IsClosing() const { return m_bIsClose; } //凡是继承了的处理功能都在这里返回this指针 virtual CefRefPtr 
      
        GetDisplayHandler() { return this; } virtual CefRefPtr 
       
         GetLifeSpanHandler() { return this; } virtual CefRefPtr 
        
          GetLoadHandler() { return this; } virtual CefRefPtr 
         
           GetContextMenuHandler() { return this; } virtual CefRefPtr 
          
            GetDownloadHandler() { return this; } // CefDisplayHandler methods: virtual void OnTitleChange(CefRefPtr 
           
             browser, const CefString& title); virtual void OnAddressChange(CefRefPtr 
            
              browser, CefRefPtr 
             
               frame, const CefString& url); virtual bool OnTooltip(CefRefPtr 
              
                browser, CefString& text); virtual void OnStatusMessage(CefRefPtr 
               
                 browser, const CefString& value); // CefLifeSpanHandler methods: virtual bool OnBeforePopup(CefRefPtr 
                
                  browser, CefRefPtr 
                 
                   frame, \ const CefString& target_url, const CefString& target_frame_name, const CefPopupFeatures& popupFeatures, \ CefWindowInfo& windowInfo, CefRefPtr 
                  
                    & client, CefBrowserSettings& settings, \ bool* no_javascript_access); virtual void OnAfterCreated(CefRefPtr 
                   
                     browser); virtual bool DoClose(CefRefPtr 
                    
                      browser); virtual void OnBeforeClose(CefRefPtr 
                     
                       browser); // CefLoadHandler methods: virtual void OnLoadStart(CefRefPtr 
                      
                        browser, CefRefPtr 
                       
                         frame); virtual void OnLoadEnd(CefRefPtr 
                        
                          browser, CefRefPtr 
                         
                           frame, int httpStatusCode); virtual void OnLoadError(CefRefPtr 
                          
                            browser, CefRefPtr 
                           
                             frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl); // Request that all existing browser windows close. void CloseAllBrowsers(bool force_close); // virtual bool OnBeforeBrowse(CefRefPtr 
                            
                              browser, CefRefPtr 
                             
                               frame, \ CefRefPtr 
                              
                                request, bool is_redirect) { //return true; return false; } virtual bool OnBeforeResourceLoad(CefRefPtr 
                               
                                 browser, CefRefPtr 
                                
                                  frame, CefRefPtr 
                                 
                                   request) { return false; } //菜单处理 virtual void OnBeforeContextMenu(CefRefPtr 
                                  
                                    browser, CefRefPtr 
                                   
                                     frame, \ CefRefPtr 
                                    
                                      params, CefRefPtr 
                                     
                                       model); virtual bool OnContextMenuCommand(CefRefPtr 
                                      
                                        browser, CefRefPtr 
                                       
                                         frame, \ CefRefPtr 
                                        
                                          params, int command_id, EventFlags event_flags); //下载处理 virtual void OnBeforeDownload( CefRefPtr 
                                         
                                           browser, CefRefPtr 
                                          
                                            download_item, \ const CefString& suggested_name, CefRefPtr 
                                           
                                             callback); virtual void OnDownloadUpdated( CefRefPtr 
                                            
                                              browser, CefRefPtr 
                                             
                                               download_item, \ CefRefPtr 
                                              
                                                callback); private: typedef std::list 
                                               
                                                 > BrowserList; BrowserList browser_list_; CefRefPtr 
                                                
                                                  m_pBrowser; bool m_bIsClose; wstring m_strHomePage; static int m_nBrowserCount; // Include the default reference counting implementation. IMPLEMENT_REFCOUNTING(CCefHandler); IMPLEMENT_LOCKING(CCefHandler); }; 
                                                 
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
  

 

 

#include "stdafx.h" #include "CefHandler.h" #include 
  
    #include "util.h" #include "../include/cef_app.h" #include "../include/cef_runnable.h" int CCefHandler::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 
   
     pMainFram=GetMainFram(); return pMainFram.get()?pMainFram->GetURL():L""; } void CCefHandler::Navigate( const wstring& strUrl ) { CefRefPtr 
    
      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); } // //菜单加载接口 void CCefHandler::OnBeforeContextMenu( CefRefPtr 
     
       browser, CefRefPtr 
      
        frame, \ CefRefPtr 
       
         params, CefRefPtr 
        
          model ) { //在这里,我添加了自己想要的菜单 cef_context_menu_type_flags_t flag = params->GetTypeFlags(); if ( flag & CM_TYPEFLAG_PAGE ) {//普通页面的右键消息 model->SetLabel(MENU_ID_BACK, L"后退"); model->SetLabel(MENU_ID_FORWARD, L"前进"); model->SetLabel(MENU_ID_VIEW_SOURCE, L"查看源代码"); model->SetLabel(MENU_ID_PRINT, L"打印"); model->SetLabel(MENU_ID_RELOAD, L"刷新"); model->SetLabel(MENU_ID_RELOAD_NOCACHE, L"强制刷新"); model->SetLabel(MENU_ID_STOPLOAD, L"停止加载"); model->SetLabel(MENU_ID_REDO, L"重复"); } if ( flag & CM_TYPEFLAG_EDITABLE) {//编辑框的右键消息 model->SetLabel(MENU_ID_UNDO, L"撤销"); model->SetLabel(MENU_ID_REDO, L"重做"); model->SetLabel(MENU_ID_CUT, L"剪切"); model->SetLabel(MENU_ID_COPY, L"复制"); model->SetLabel(MENU_ID_PASTE, L"粘贴"); model->SetLabel(MENU_ID_DELETE, L"删除"); model->SetLabel(MENU_ID_SELECT_ALL, L"全选"); } } bool CCefHandler::OnContextMenuCommand( CefRefPtr 
         
           browser, CefRefPtr 
          
            frame, \ CefRefPtr 
           
             params, int command_id, EventFlags event_flags ) { return false; } // //网页加载状态回调接口 void CCefHandler::OnLoadStart( CefRefPtr 
            
              browser, CefRefPtr 
             
               frame ) { } void CCefHandler::OnLoadEnd( CefRefPtr 
              
                browser, CefRefPtr 
               
                 frame, int httpStatusCode ) { //CefRefPtr 
                
                  v8 = frame->GetV8Context(); } void CCefHandler::OnLoadError(CefRefPtr 
                 
                   browser, CefRefPtr 
                  
                    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::wstring ss << L" " L" 
                   

Failed to load URL " << std::wstring(failedUrl) << L" with error " << std::wstring(errorText) << L" (" << errorCode << L").

"<<'\0'; frame->LoadString(ss.str(), failedUrl); } // //状态改变回调接口 void CCefHandler::OnTitleChange( CefRefPtr browser, const CefString& title ) { } void CCefHandler::OnAddressChange( CefRefPtr browser, CefRefPtr frame, const CefString& url ) { } bool CCefHandler::OnTooltip( CefRefPtr browser, CefString& text ) { return false; } void CCefHandler::OnStatusMessage( CefRefPtr browser, const CefString& value ) { } // //网页生命周期回调接口 bool CCefHandler::OnBeforePopup( CefRefPtr browser, CefRefPtr frame, \ const CefString& target_url, const CefString& target_frame_name, const CefPopupFeatures& popupFeatures, \ CefWindowInfo& windowInfo, CefRefPtr & client, CefBrowserSettings& settings, \ bool* no_javascript_access ) { //这里使用默认浏览器打开网页,避免CEF重新创建窗口 ShellExecute(NULL, L"open", target_url.c_str(), NULL, NULL, SW_SHOW); //return false;//创建新窗口 return true; //禁止创建新的窗口 } void CCefHandler::OnAfterCreated(CefRefPtr browser) { REQUIRE_UI_THREAD(); // Add to the list of existing browsers. browser_list_.push_back(browser); AutoLock scopLock(this); if ( ! m_pBrowser.get() ) m_pBrowser=browser; m_nBrowserCount++; } bool CCefHandler::DoClose(CefRefPtr browser) { REQUIRE_UI_THREAD(); // Closing the main window requires special handling. See the DoClose() // documentation in the CEF header for a detailed destription of this // process. if (browser_list_.size() == 1) { // Set a flag to indicate that the window close should be allowed. m_bIsClose = true; } // Allow the close. For windowed browsers this will result in the OS close // event being sent. return false; } void CCefHandler::OnBeforeClose(CefRefPtr browser) { REQUIRE_UI_THREAD(); // Remove from the list of existing browsers. BrowserList::iterator bit = browser_list_.begin(); for (; bit != browser_list_.end(); ++bit) { if ((*bit)->IsSame(browser)) { browser_list_.erase(bit); break; } } if (browser_list_.empty()) { // All browser windows have closed. Quit the application message loop. CefQuitMessageLoop(); } } void CCefHandler::OnBeforeDownload( CefRefPtr browser, CefRefPtr download_item, \ const CefString& suggested_name, CefRefPtr callback ) { int i=0; } void CCefHandler::OnDownloadUpdated( CefRefPtr browser, CefRefPtr download_item, \ CefRefPtr callback ) { //取消CEF内部下载文件,使用默认浏览器打开链接去下载,下载过程就不需要我们关心了,毕竟不是做浏览器 callback->Cancel(); CefString strUrl = download_item->GetURL(); ShellExecute(NULL, L"open", strUrl.c_str(), NULL, NULL, SW_SHOW); int i = 0; }

 

使用时,创建一个win32项目,然后修改WinMain函数:

CefRefPtr 
  
    g_handler; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: 在此放置代码。 CefMainArgs main_args(hInstance); CefRefPtr 
   
     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; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_CEFDEMO, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CEFDEMO)); CefRunMessageLoop(); CefShutdown(); return 0; } 
    
  

消息处理主要是创建、销毁CEF对象,窗口大小改变时通知CEF窗口等:

 

 

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/ g_handler=new CCefHandler(L"www..com"); RECT rect; ::GetClientRect(hWnd, &rect); g_handler->CreateBrowser(hWnd, rect); break; } case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; default: break; } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: 在此添加任意绘图代码... 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 
  
    pBrowser=g_handler->GetBrowser(); if ( pBrowser.get() ) pBrowser->GetHost()->CloseBrowser(true); } break; } default: break; } return DefWindowProc(hWnd, message, wParam, lParam); } 
  

编译,运行程序,一个简单的网页就出来了,加载速度比IE那垃圾快多了,关键是不用理会兼容性问题了。

 

Windows上使用CEF嵌入基于chrome内核浏览器小例
 

最后记得带上CEF的一大堆DLL,这个是必须的,如果网页需要播放视频,需要新建一个plugins文件夹,放入视频播放插件NPSWF32.dll。

需要链接的CEF动态链接库:

 

#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

libcef使用范例之4399网页小游戏,git源码:https://github.com/JelinYao/4399WebGames

使用libCef+Duilib开发属于自己的浏览器已开源:https://github.com/JelinYao/MyChrome

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210830.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:49
下一篇 2026年3月18日 下午11:50


相关推荐

  • java异常中throw和throws的区别

    java异常中throw和throws的区别throws 和 throwthrows 用来声明一个方法可能产生的所有异常 不做任何处理而是将异常往上传 谁调用我我就抛给谁 nbsp nbsp 用在方法声明后面 跟的是异常类名 nbsp nbsp 可以跟多个异常类名 用逗号隔开 nbsp nbsp 表示抛出异常 由该方法的调用者来处理 nbsp nbsp throws 表示出现异常的一种可能性 并不一定会发生这些异常 throw 则是用来抛出一个具体的异常类型 nbsp nbsp 用在方法体内 跟的是异常对

    2026年3月19日
    1
  • 网站前端和后台性能优化21

    网站前端和后台性能优化21

    2022年3月13日
    44
  • height:100%失败

    height:100%失败

    2022年1月1日
    42
  • 数据库主键和外键的作用_数据库外键约束的作用

    数据库主键和外键的作用_数据库外键约束的作用主键保证了数据的唯一性,外键保证了数据的完整性。主键是能确定一条记录的唯一标识,比如,一条记录包括身份正号,姓名,年龄。身份证号是唯一能确定你这个人的,其他都可能有重复,所以,身份证号是主键。外键用于与另一张表的关联。是能确定另一张表记录的字段,用于保持数据的一致性。比如,A表中的一个字段,是B表的主键,那他就可以是A表的外键。…

    2025年8月2日
    5
  • mysql存储过程菜鸟教程_mysql存储过程是什么

    mysql存储过程菜鸟教程_mysql存储过程是什么本文介绍关于在MySQL存储过程游标使用实例,包括简单游标使用与游标循环跳出等方法例1、一个简单存储过程游标实例DELIMITER$$DROPPROCEDUREIFEXISTSgetUserInfo$$CREATEPROCEDUREgetUserInfo(indate_daydatetime)—-实例–存储过程名为:getUserInfo–参数为:date_day…

    2022年10月1日
    5
  • Java中的队列[通俗易懂]

    Java中的队列[通俗易懂]目录参考Deque从初学者的角度,认真地学习Java中队列的使用和设计。参考javadocDeque一个支持两端插入和删除的线性集合,此接口支持容量受限和不受限的双端队列(大多数实现容量不受限)。该接口定义了访问两端元素的方法,主要是插入、删除、检查元素方法。这些方法主要有两种形式,一种在操作失败时引发异常,一种在操作失败时返回特殊值(null或者false)。这里着重提一下插入操作,只有当队列容量受限时,插入操作才可能失败。12个方法如下该接口扩展了Queue接口。当双端队列

    2022年7月9日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号