在mfc单文档程序中加入cef:
.在BOOL CtestCEFApp::InitInstance()中初始化cef
HINSTANCE hInst = GetModuleHandle(NULL);
CefMainArgs main_args(hInst); m_cefApp = new ClientApp(); //cef默认启动四个进程,分别是浏览器主进程,渲染进程,GPU进程,插件进程,如果不是主进程则直接退出
//AfxMessageBox(L"CefExecuteProcess");
int exit_code = CefExecuteProcess(main_args, m_cefApp.get(), NULL);
if (exit_code >= )
return exit_code; //设置cef参数
CString szCEFCache;
CString szPath;
INT nLen = GetTempPath( , NULL ) + ;
GetTempPath( nLen, szPath.GetBuffer( nLen ));
szCEFCache.Format( _T("%scache\0\0"), szPath ); CefSettings settings;
//settings.no_sandbox = TRUE;
//settings.multi_threaded_message_loop = FALSE;
CefString(&settings.cache_path) = szCEFCache; //CefSettingsTraits::init( &cSettings);
//cSettings.multi_threaded_message_loop = false; m_bCEFInitialized = CefInitialize(main_args, settings,m_cefApp.get(), NULL); //初始化自定义协议
scheme_test::InitTest();
.创建浏览器窗口
BOOL ClientApp::CreateBrowser(HWND hWnd, CRect rect, LPCTSTR pszURL)
{
CefBrowserSettings settings;
CefWindowInfo info; info.SetAsChild( hWnd, rect );
std::string url = "test://test/html1.html"; return CefBrowserHost::CreateBrowser( info, m_cefHandler.get(), url, settings, NULL );
}
.资源文件加载
namespace scheme_test { // Implementation of the factory for for creating schema handlers.
class ClientSchemeHandlerFactory : public CefSchemeHandlerFactory {
public:
// Return a new scheme handler instance to handle the request.
virtual CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& scheme_name,
CefRefPtr<CefRequest> request)
OVERRIDE {
return NULL;
} IMPLEMENT_REFCOUNTING(ClientSchemeHandlerFactory);
}; //此方法在ClientApp的虚函数中被调用
void RegisterCustomSchemes(CefRefPtr<CefSchemeRegistrar> registrar,
std::vector<CefString>& cookiable_schemes) {
registrar->AddCustomScheme("test", true, false, false);//注册一个新的协议test
} //初始化时调用
void InitTest() {
CefRegisterSchemeHandlerFactory("test", "test", new ClientSchemeHandlerFactory());//注册一个新的域名,并且新建一个可以处理此域名请求的类
} }  // namespace scheme_test
.两个关键的类
class CWebClient
: public CefClient
, public CefLifeSpanHandler
, public CefRequestHandler
{ public:
CMyWebBrowser m_wndBrowser;
public:
CWebClient(void){}
virtual ~CWebClient(void){} virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() { return this; }
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() { return this; } //virtual bool DoClose( CefRefPtr<CefBrowser> browser );
//virtual void OnBeforeClose( CefRefPtr<CefBrowser> browser );
virtual void OnAfterCreated( CefRefPtr<CefBrowser> browser ); //此处处理接收到的message
virtual bool OnProcessMessageReceived( CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message );
//此处加载本地html资源
virtual CefRefPtr<CefResourceHandler> GetResourceHandler( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request ); private:
bool ParseTestUrl(const std::string& url, std::string* file_name, std::string* mime_type); public: IMPLEMENT_REFCOUNTING(CWebClient);
IMPLEMENT_LOCKING(CWebClient);
};
class ClientApp: public CefApp
,public CefBrowserProcessHandler
,public CefRenderProcessHandler
{
public:
ClientApp(void);
ClientApp(HWND hWnd);
~ClientApp(void); virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler()OVERRIDE { return this; }
//此处处理js和c++调用
virtual void OnContextCreated( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context ); virtual void OnContextInitialized() OVERRIDE; //此处要<span style="font-family: Arial, Helvetica, sans-serif;">注册一个新的协议test</span>
virtual void OnRegisterCustomSchemes( CefRefPtr<CefSchemeRegistrar> registrar ); BOOL CreateBrowser(HWND hWnd, CRect rect, LPCTSTR pszURL); public:
CefRefPtr<CWebClient> m_cefHandler; private:
IMPLEMENT_REFCOUNTING(ClientApp); };
.c++和js相互调用
第一种:
class CefV8ExtensionHandler : public CefV8Handler
{
public:
CefV8ExtensionHandler(){}
~CefV8ExtensionHandler(){} virtual bool Execute( const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception ); IMPLEMENT_REFCOUNTING(CefV8ExtensionHandler);
};
void ClientApp::OnContextCreated( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context )
{
CefRefPtr<CefV8Value> obj = context->GetGlobal(); CefRefPtr<CefV8Handler> handler = new CefV8ExtensionHandler(); obj->SetValue("test", CefV8Value::CreateFunction("test", handler), V8_PROPERTY_ATTRIBUTE_READONLY); }
bool CefV8ExtensionHandler::Execute( const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception )
{
if (name == "test")
{
std::string arg1 = arguments[]->GetStringValue(); //MessageBoxA(NULL, arg1.c_str(), NULL, NULL); retval = CefV8Value::CreateString(arg1); CefRefPtr<CefBrowser> browser = CefV8Context::GetCurrentContext()->GetBrowser();
ASSERT(browser.get()); CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("test"); browser->SendProcessMessage(PID_BROWSER, message); return true;
} return false;
}
第二种:
std::string app_code =
"var app;"
"if (!app)"
"  app = {};"
"(function() {"
"  app.sendMessage = function(name, arguments) {"
"    native function sendMessage();"
"    return sendMessage(name, arguments);"
"  };"
"  app.setMessageCallback = function(name, callback) {"
"    native function setMessageCallback();"
"    return setMessageCallback(name, callback);"
"  };"
"  app.removeMessageCallback = function(name) {"
"    native function removeMessageCallback();"
"    return removeMessageCallback(name);"
"  };"
"})();";
CefRegisterExtension("v8/app", app_code,
new ClientAppExtensionHandler(this));
.自定义标题栏需要子类化浏览器窗口方能接收到消息
class CMyWebBrowser : public CWnd
{
DECLARE_DYNCREATE(CMyWebBrowser) public:
CMyWebBrowser(void);
virtual ~CMyWebBrowser(void); protected:
DECLARE_MESSAGE_MAP() public:
afx_msg LRESULT OnNcHitTest(CPoint point);
};
LRESULT CMyWebBrowser::OnNcHitTest(CPoint point)
{
CRect rc;
GetClientRect(&rc);
rc.top = rc.top;
rc.left = rc.left;
rc.right = rc.right;
rc.bottom = ;
ClientToScreen(&rc); CPoint screenpoint(point);
ScreenToClient(&screenpoint); if (rc.PtInRect(point))
{
AfxGetApp()->GetMainWnd()->PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(screenpoint.x, screenpoint.y)); return ;
}
else
return CWnd::OnNcHitTest(point);
}
void CWebClient::OnAfterCreated( CefRefPtr<CefBrowser> browser )
{
HWND hWnd = browser->GetHost()->GetWindowHandle();
HWND hChildWnd = ::GetNextWindow(hWnd, GW_CHILD );
m_wndBrowser.SubclassWindow(hChildWnd); CefLifeSpanHandler::OnAfterCreated(browser);
}
.消息循环
int CtestCEFApp::ExitInstance()
{
if( m_bCEFInitialized )
{
m_bCEFInitialized = false; m_cefApp = NULL; CefShutdown();
} return CWinAppEx::ExitInstance();
} BOOL CtestCEFApp::PumpMessage()
{
if( m_bCEFInitialized )
CefDoMessageLoopWork(); return CWinAppEx::PumpMessage();
} --------------------- 本文来自 lls2012 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/lls2012/article/details/47976101?utm_source=copy

https://blog.csdn.net/lls2012/article/details/47976101

在mfc单文档程序中加入cef: 1.在BOOL CtestCEFApp::InitInstance()中初始化cef HINSTANCE hInst = GetModuleHandle(NULL); CefMainArgs main_args(hInst); m_cefApp = new ClientApp(); //cef默认启动四个进程,分别是浏览器主进程,渲染进程,GPU进程,插件进程,如果不是主进程则直接退出 //AfxMessageBox(L"CefExecuteProcess"); int exit_code = CefExecuteProcess(main_args, m_cefApp.get(), NULL); if (exit_code >= 0) return exit_code; //设置cef参数 CString szCEFCache; CString szPath; INT nLen = GetTempPath( 0, NULL ) + 1; GetTempPath( nLen, szPath.GetBuffer( nLen )); szCEFCache.Format( _T("%scache\0\0"), szPath ); CefSettings settings; //settings.no_sandbox = TRUE; //settings.multi_threaded_message_loop = FALSE; CefString(&settings.cache_path) = szCEFCache; //CefSettingsTraits::init( &cSettings); //cSettings.multi_threaded_message_loop = false; m_bCEFInitialized = CefInitialize(main_args, settings,m_cefApp.get(), NULL); //初始化自定义协议 scheme_test::InitTest(); 2.创建浏览器窗口 BOOL ClientApp::CreateBrowser(HWND hWnd, CRect rect, LPCTSTR pszURL) { CefBrowserSettings settings; CefWindowInfo info; info.SetAsChild( hWnd, rect ); std::string url = "test://test/html1.html"; return CefBrowserHost::CreateBrowser( info, m_cefHandler.get(), url, settings, NULL ); } 3.资源文件加载 namespace scheme_test { // Implementation of the factory for for creating schema handlers. class ClientSchemeHandlerFactory : public CefSchemeHandlerFactory { public: // Return a new scheme handler instance to handle the request. virtual CefRefPtr Create(CefRefPtr browser, CefRefPtr frame, const CefString& scheme_name, CefRefPtr request) OVERRIDE { return NULL; } IMPLEMENT_REFCOUNTING(ClientSchemeHandlerFactory); }; //此方法在ClientApp的虚函数中被调用 void RegisterCustomSchemes(CefRefPtr registrar, std::vector& cookiable_schemes) { registrar->AddCustomScheme("test", true, false, false);//注册一个新的协议test } //初始化时调用 void InitTest() { CefRegisterSchemeHandlerFactory("test", "test", new ClientSchemeHandlerFactory());//注册一个新的域名,并且新建一个可以处理此域名请求的类 } }  // namespace scheme_test 4.两个关键的类 class CWebClient : public CefClient , public CefLifeSpanHandler , public CefRequestHandler { public: CMyWebBrowser m_wndBrowser; public: CWebClient(void){} virtual ~CWebClient(void){} virtual CefRefPtr GetLifeSpanHandler() { return this; } virtual CefRefPtr GetRequestHandler() { return this; } //virtual bool DoClose( CefRefPtr browser ); //virtual void OnBeforeClose( CefRefPtr browser ); virtual void OnAfterCreated( CefRefPtr browser ); //此处处理接收到的message virtual bool OnProcessMessageReceived( CefRefPtr browser, CefProcessId source_process, CefRefPtr message ); //此处加载本地html资源 virtual CefRefPtr GetResourceHandler( CefRefPtr browser, CefRefPtr frame, CefRefPtr request ); private: bool ParseTestUrl(const std::string& url, std::string* file_name, std::string* mime_type); public: IMPLEMENT_REFCOUNTING(CWebClient); IMPLEMENT_LOCKING(CWebClient); }; class ClientApp: public CefApp ,public CefBrowserProcessHandler ,public CefRenderProcessHandler { public: ClientApp(void); ClientApp(HWND hWnd); ~ClientApp(void); virtual CefRefPtr GetBrowserProcessHandler() OVERRIDE { return this; } virtual CefRefPtr GetRenderProcessHandler()OVERRIDE { return this; } //此处处理js和c++调用 virtual void OnContextCreated( CefRefPtr browser, CefRefPtr frame, CefRefPtr context ); virtual void OnContextInitialized() OVERRIDE; //此处要注册一个新的协议test virtual void OnRegisterCustomSchemes( CefRefPtr registrar ); BOOL CreateBrowser(HWND hWnd, CRect rect, LPCTSTR pszURL); public: CefRefPtr m_cefHandler; private: IMPLEMENT_REFCOUNTING(ClientApp); }; 5.c++和js相互调用 第一种: class CefV8ExtensionHandler : public CefV8Handler { public: CefV8ExtensionHandler(){} ~CefV8ExtensionHandler(){} virtual bool Execute( const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception ); IMPLEMENT_REFCOUNTING(CefV8ExtensionHandler); }; void ClientApp::OnContextCreated( CefRefPtr browser, CefRefPtr frame, CefRefPtr context ) { CefRefPtr obj = context->GetGlobal(); CefRefPtr handler = new CefV8ExtensionHandler(); obj->SetValue("test", CefV8Value::CreateFunction("test", handler), V8_PROPERTY_ATTRIBUTE_READONLY); } bool CefV8ExtensionHandler::Execute( const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception ) { if (name == "test") { std::string arg1 = arguments[0]->GetStringValue(); //MessageBoxA(NULL, arg1.c_str(), NULL, NULL); retval = CefV8Value::CreateString(arg1); CefRefPtr browser = CefV8Context::GetCurrentContext()->GetBrowser(); ASSERT(browser.get()); CefRefPtr message = CefProcessMessage::Create("test"); browser->SendProcessMessage(PID_BROWSER, message); return true; } return false; } 第二种: std::string app_code = "var app;" "if (!app)" "  app = {};" "(function() {" "  app.sendMessage = function(name, arguments) {" "    native function sendMessage();" "    return sendMessage(name, arguments);" "  };" "  app.setMessageCallback = function(name, callback) {" "    native function setMessageCallback();" "    return setMessageCallback(name, callback);" "  };" "  app.removeMessageCallback = function(name) {" "    native function removeMessageCallback();" "    return removeMessageCallback(name);" "  };" "})();"; CefRegisterExtension("v8/app", app_code, new ClientAppExtensionHandler(this)); 6.自定义标题栏需要子类化浏览器窗口方能接收到消息 class CMyWebBrowser : public CWnd { DECLARE_DYNCREATE(CMyWebBrowser) public: CMyWebBrowser(void); virtual ~CMyWebBrowser(void); protected: DECLARE_MESSAGE_MAP() public: afx_msg LRESULT OnNcHitTest(CPoint point); }; LRESULT CMyWebBrowser::OnNcHitTest(CPoint point) { CRect rc; GetClientRect(&rc); rc.top = rc.top; rc.left = rc.left; rc.right = rc.right; rc.bottom = 20; ClientToScreen(&rc); CPoint screenpoint(point); ScreenToClient(&screenpoint); if (rc.PtInRect(point)) { AfxGetApp()->GetMainWnd()->PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(screenpoint.x, screenpoint.y)); return 0; } else return CWnd::OnNcHitTest(point); } void CWebClient::OnAfterCreated( CefRefPtr browser ) { HWND hWnd = browser->GetHost()->GetWindowHandle(); HWND hChildWnd = ::GetNextWindow(hWnd, GW_CHILD ); m_wndBrowser.SubclassWindow(hChildWnd); CefLifeSpanHandler::OnAfterCreated(browser); } 7.消息循环 int CtestCEFApp::ExitInstance() { if( m_bCEFInitialized ) { m_bCEFInitialized = false; m_cefApp = NULL; CefShutdown(); } return CWinAppEx::ExitInstance(); } BOOL CtestCEFApp::PumpMessage() { if( m_bCEFInitialized ) CefDoMessageLoopWork(); return CWinAppEx::PumpMessage(); } --------------------- 本文来自 lls2012 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/lls2012/article/details/47976101?utm_source=copy

mfc cef<转>的更多相关文章

  1. 转:CEF嵌入到单文档mfc

    1.下载: http://www.magpcss.net/cef_downloads/下载cef binary 1.1364.1123 windows.zip(可能要FQ,百度goagent教程,最好 ...

  2. mfc封装cef浏览器 关闭整个窗口程序得时候又重启mfc 应用的程序

    最近使用mfc 做了一个cef得浏览器 多标签得.当使用这个封装得浏览器一段时间之后(超过1分钟2分钟) 当关闭封装得浏览器整个窗体 x得时候,整个窗体又重新弹了出来. 大概现象就是一个exe程序你杀 ...

  3. MFC调用CEF实现单页面单COOKIE管理《转》

    cookie简单介绍 cookie存储了网站的一些很重要的信息,如用户身份信息.常用设置.设置地理位置等等各种信息.使用cef访问网站时,如果设置了CefSettings.cache_path参数,则 ...

  4. CEF源码编译和生产库的使用

    CEF版本是Branch 2171 开发环境是VS2012 查看一下libcef_dll_wrapper工程属性,确定Code Generation 选择MTD(Debug) 或者MT(Release ...

  5. CEF使用的几个注意点

    CEF为chrome浏览器的切入其他浏览器中的轻量级框架. 开发的客户端的时候,这是作为界面显示的首先,可以增强客户的易变性,可塑性. 在开发的过程中(侧重于C,C++解决),遇到的几个问题,以及自己 ...

  6. CEF 相关资料

    理解WebKit和Chromium: Content API和CEF3 http://blog.csdn.net/milado_nju/article/details/7455373 如何将Chrom ...

  7. 【转】MFC内嵌cef3浏览器内核

    一.cef3内核的下载 可以从http://opensource.spotify.com/cefbuilds/index.html下载,注意:很多版本编译都可以通过 但是运行的时候会崩溃,以cef_b ...

  8. Duilib嵌入CEF出现窗口显示不正常

    参考资料:https://www.aliyun.com/zixun/wenji/1247250.html 转载:https://www.cnblogs.com/gongxijun/p/4857977. ...

  9. 笨重的mfc还在基于系统控件,熟练的mfc工程师还比不过学习Qt一个月的学生开发效率高(比较精彩,韦易笑)

    作者:韦易笑链接:https://www.zhihu.com/question/29636221/answer/45102191来源:知乎著作权归作者所有,转载请联系作者获得授权. 更新:擦,本来只有 ...

随机推荐

  1. JavaScript和HTML DOM的区别与联系

    JavaScript和HTML DOM的区别与联系 区别: javascript JavaScript 是因特网上最流行的浏览器脚本语言.很容易使用!你一定会喜欢它的! JavaScript 被数百万 ...

  2. [UE4]创建Shooter基类,2种方法

    一.可以通过直接修改"BP_FPPCharacter"的名字为“BP_Shooter”作为基类,然后新建一个"BP_FPPCharacter"继承自“BP_Sh ...

  3. win下使用git-bash工具进行ssh免密登录服务器

    1.ssh-keygen.exe 生成公钥私钥(.pub) 2.ssh-agent.exe bash  指定工具 3.ssh-add.exe   ****   添加私钥 OK

  4. xml布局解析报错的可能原因

    xml布局解析报如下的错11-15 16:55:21.425 17633-17633/com.hongfans.mobileconnect I/LogUtils_info: [CrashHandler ...

  5. POJ Lost Cows

    [题解] 参考https://blog.csdn.net/acmer_hades/article/details/46272605.设置数组pre_smaller,其中第i个元素即为输入的第i项,则显 ...

  6. 我的常用笔记(GetAndroid,ADBDemo,GetSJ,GetTB)

    一.授权相关格式(GetAndroid,ADBDemo,GetTB,GetSJ) [Mac]ID=0,     Mac=9918D2A363,    EndTime=2018-12-30 15:45: ...

  7. C++使用Mysql的详细步骤及各个常用方法的代码演示:select,insert,update,delete

    这几天一直在学习C++下使用Mysql的方法及其中各种的问题,也看了很多Mysql的API函数,当然自己看的还是很基础的.其实对于每种数据库的操作,基本的方法都是非常类似的,大多都是connect,s ...

  8. MapReduce的工作机制

    <Hadoop权威指南>中的MapReduce工作机制和Shuffle: 框架 Hadoop2.x引入了一种新的执行机制MapRedcue 2.这种新的机制建议在Yarn的系统上,目前用于 ...

  9. Java - 19 Java 异常处理

    Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用 ...

  10. Getting Physical With Memory.CPU如何操作内存

    原文标题:Getting Physical With Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译一下 ...