MFC对话框应用程序中谷歌CEF浏览器内核的使用
2015-09-01 16:20
447 查看
MFC中支持IE浏览器内核的Microsoft Web Browser控件:http://blog.csdn.net/one_in_one/article/details/26372981
谷歌CEF浏览器内核参考:http://m.blog.csdn.net/blog/wmushao999/37606189
最近做的MFC项目要求对话框中需要显示网页图片,可以选择使用IE或谷歌内核。对于IE内核可以直接使用MFC中的ACTIVEX控件,但是对于谷歌浏览器内核却并没有这么现成的控件可以使用。CEF是谷歌对WebKit的一个封装,可百度搜索相关资料。IE内核的Microsoft Web Browser控件对网页的兼容性不好,因而选择了谷歌的CEF,本文参考以上谷歌浏览器内核使用http://m.blog.csdn.net/blog/wmushao999/37606189
相关如下:
1、下载CEF源代码。这里是第一个注意点。CEF分为cef1、cef2和cef3三个版本,其中cef1为单线程版本,cef2已经放弃开发,cef3为多线程版本。本教程适用于cef1,至于cef3没有测试过,不知道能不能使。
2、CEF编译。下载好CEF后,解压,设置libcef_dll_wrapper为启动项分别生成debug和release两个版本的libcef_dll_wrapper.lib。在生成后VS会提示无法运行libcef_dll_wrapper.lib,不用理会。运行生成后在生成目录下找到生成的debug和release版本的libcef_dll_wrapper.lib.注意生成路径为目录bulid
此时,cef相关文件的生成工作已经完成。需要从程序中找出来以备后用的文件如下:
…\cef_binary_1.1364.1123_windows\include文件夹
…\cef_binary_1.1364.1123_windows\lib\Debug\ libcef_dll-wrapper.lib
…\cef_binary_1.1364.1123_windows\lib\Release\ libcef_dll-wrapper.lib(与上面同名用不同的文件夹分开放)
…\cef_binary_1.1364.1123_windows\Debug文件夹(要用的是除cefclient以外的所有文件)
...\cef_binary_1.1364.1123_windows\cef_binary_1.1364.1123_windows\lib\Release\libcef.lib
至此准备工作完成。
3、在MFC对话框项目中使用CEF
(1)、建立一个MFC对话框项目
(2)、环境配置
将准备的libcef.lib、libcef_dll_wrapper.lib(在debug时工程目录下放的是debug版本的,当release时替换为release的)、include文件夹一同复制到工程目录下
a.设置项目链接库有:libcef.lib和libcef_dll_wrapper.lib
b.配置属性->预编译头->预编译头->不适用预编译头
c.配置属性->c/c++->代码生成->运行库->多线程调试(/MTd)(realse下选择多线程MT)
d.配置属性->常规->MFC的使用->在静态库中使用MFC
(3)、添加cwebclient类
在菜单栏选择:项目->类向导->添加类->输入类名->完成
把CWebClient.h的内容改为如下:
#pragma once
#include <cef_client.h>
class CWebClient : public CefClient
, public CefLifeSpanHandler
{
protected:
CefRefPtr<CefBrowser> m_Browser;
public:
CWebClient(void){};
virtual ~CWebClient(void){};
CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE{ return this; }
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
// 添加CEF的SP虚函数
IMPLEMENT_REFCOUNTING(CWebClient);
IMPLEMENT_LOCKING(CWebClient);
};
在CWebClient.cpp中加入如下代码(覆盖):
#include "stdafx.h"
#include "CWebClient.h"
void CWebClient::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
m_Browser = browser;
}
(4)在主窗体的.h文件中加入
#include <cef_client.h>
#include "CWebClient.h"
#include <cef_app.h>
在类的public成员中添加
CefRefPtr<CWebClient> m_cWebClient;
(5)给出创建浏览器的代码
CefRefPtr<CWebClient>client(new CWebClient());
m_cWebClient= client;
CefSettings cSettings;
CefSettingsTraits::init(&cSettings);
cSettings.multi_threaded_message_loop= true;
CefRefPtr<CefApp>spApp;
CefInitialize(cSettings, spApp);
CefWindowInfo info;
RECT rect;
GetClientRect(&rect);
RECT rectnew=rect;
rectnew.top=rect.top+70;
rectnew.bottom=rect.bottom;
rectnew.left=rect.left;
rectnew.right=rect.right;
info.SetAsChild(GetSafeHwnd(),rectnew);
CefBrowserSettings browserSettings;
CefBrowser::CreateBrowser(info, static_cast<CefRefPtr<CefClient> >(client),"http://www.baidu.com",browserSettings);
(6)显示下一个网页
CefRefPtr<CefFrame> pMainFram = m_cWebClient->GetBrowser()->GetMainFrame();
pMainFram->LoadURL((CefString)strURL);
(7)浏览器隐藏与显示
::ShowWindow(m_cWebClient->GetBrowser()->GetWindowHandle(),SW_HIDE);
::ShowWindow(m_cWebClient->GetBrowser()->GetWindowHandle(),SW_SHOW);
(8)在onsize()函数体中写入如下代码
void CMfcBrowserDlg::OnSize(UINT nType, int cx,int cy)
{
CDialogEx::OnSize(nType,cx,cy);
//TODO: 在此处添加消息处理程序代码
if(m_cWebClient.get())
{
CefRefPtr<CefBrowser>browser = m_cWebClient->GetBrowser();
if(browser)
{
CefWindowHandle hwnd = browser->GetWindowHandle();
::MoveWindow(hwnd,100,100,800,800, true);
}
}
}
谷歌CEF浏览器内核参考:http://m.blog.csdn.net/blog/wmushao999/37606189
最近做的MFC项目要求对话框中需要显示网页图片,可以选择使用IE或谷歌内核。对于IE内核可以直接使用MFC中的ACTIVEX控件,但是对于谷歌浏览器内核却并没有这么现成的控件可以使用。CEF是谷歌对WebKit的一个封装,可百度搜索相关资料。IE内核的Microsoft Web Browser控件对网页的兼容性不好,因而选择了谷歌的CEF,本文参考以上谷歌浏览器内核使用http://m.blog.csdn.net/blog/wmushao999/37606189
相关如下:
1、下载CEF源代码。这里是第一个注意点。CEF分为cef1、cef2和cef3三个版本,其中cef1为单线程版本,cef2已经放弃开发,cef3为多线程版本。本教程适用于cef1,至于cef3没有测试过,不知道能不能使。
2、CEF编译。下载好CEF后,解压,设置libcef_dll_wrapper为启动项分别生成debug和release两个版本的libcef_dll_wrapper.lib。在生成后VS会提示无法运行libcef_dll_wrapper.lib,不用理会。运行生成后在生成目录下找到生成的debug和release版本的libcef_dll_wrapper.lib.注意生成路径为目录bulid
此时,cef相关文件的生成工作已经完成。需要从程序中找出来以备后用的文件如下:
…\cef_binary_1.1364.1123_windows\include文件夹
…\cef_binary_1.1364.1123_windows\lib\Debug\ libcef_dll-wrapper.lib
…\cef_binary_1.1364.1123_windows\lib\Release\ libcef_dll-wrapper.lib(与上面同名用不同的文件夹分开放)
…\cef_binary_1.1364.1123_windows\Debug文件夹(要用的是除cefclient以外的所有文件)
...\cef_binary_1.1364.1123_windows\cef_binary_1.1364.1123_windows\lib\Release\libcef.lib
至此准备工作完成。
3、在MFC对话框项目中使用CEF
(1)、建立一个MFC对话框项目
(2)、环境配置
将准备的libcef.lib、libcef_dll_wrapper.lib(在debug时工程目录下放的是debug版本的,当release时替换为release的)、include文件夹一同复制到工程目录下
a.设置项目链接库有:libcef.lib和libcef_dll_wrapper.lib
b.配置属性->预编译头->预编译头->不适用预编译头
c.配置属性->c/c++->代码生成->运行库->多线程调试(/MTd)(realse下选择多线程MT)
d.配置属性->常规->MFC的使用->在静态库中使用MFC
(3)、添加cwebclient类
在菜单栏选择:项目->类向导->添加类->输入类名->完成
把CWebClient.h的内容改为如下:
#pragma once
#include <cef_client.h>
class CWebClient : public CefClient
, public CefLifeSpanHandler
{
protected:
CefRefPtr<CefBrowser> m_Browser;
public:
CWebClient(void){};
virtual ~CWebClient(void){};
CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE{ return this; }
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
// 添加CEF的SP虚函数
IMPLEMENT_REFCOUNTING(CWebClient);
IMPLEMENT_LOCKING(CWebClient);
};
在CWebClient.cpp中加入如下代码(覆盖):
#include "stdafx.h"
#include "CWebClient.h"
void CWebClient::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
m_Browser = browser;
}
(4)在主窗体的.h文件中加入
#include <cef_client.h>
#include "CWebClient.h"
#include <cef_app.h>
在类的public成员中添加
CefRefPtr<CWebClient> m_cWebClient;
(5)给出创建浏览器的代码
CefRefPtr<CWebClient>client(new CWebClient());
m_cWebClient= client;
CefSettings cSettings;
CefSettingsTraits::init(&cSettings);
cSettings.multi_threaded_message_loop= true;
CefRefPtr<CefApp>spApp;
CefInitialize(cSettings, spApp);
CefWindowInfo info;
RECT rect;
GetClientRect(&rect);
RECT rectnew=rect;
rectnew.top=rect.top+70;
rectnew.bottom=rect.bottom;
rectnew.left=rect.left;
rectnew.right=rect.right;
info.SetAsChild(GetSafeHwnd(),rectnew);
CefBrowserSettings browserSettings;
CefBrowser::CreateBrowser(info, static_cast<CefRefPtr<CefClient> >(client),"http://www.baidu.com",browserSettings);
(6)显示下一个网页
CefRefPtr<CefFrame> pMainFram = m_cWebClient->GetBrowser()->GetMainFrame();
pMainFram->LoadURL((CefString)strURL);
(7)浏览器隐藏与显示
::ShowWindow(m_cWebClient->GetBrowser()->GetWindowHandle(),SW_HIDE);
::ShowWindow(m_cWebClient->GetBrowser()->GetWindowHandle(),SW_SHOW);
(8)在onsize()函数体中写入如下代码
void CMfcBrowserDlg::OnSize(UINT nType, int cx,int cy)
{
CDialogEx::OnSize(nType,cx,cy);
//TODO: 在此处添加消息处理程序代码
if(m_cWebClient.get())
{
CefRefPtr<CefBrowser>browser = m_cWebClient->GetBrowser();
if(browser)
{
CefWindowHandle hwnd = browser->GetWindowHandle();
::MoveWindow(hwnd,100,100,800,800, true);
}
}
}
相关文章推荐
- Android Manifest 用法
- Android学习笔记(二九):嵌入浏览器
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 浏览器 cookie 限制
- flex 控件的重要属性
- 谷歌、雅虎支持中文域名搜索 有助提升搜索引擎优化
- 玩转浏览器IE7的5个顶级使用技巧
- Delphi控件ListView的属性及使用方法详解
- 字符集导致的浏览器跨站脚本攻击分析
- web下载的ActiveX控件自动更新
- 更改IE浏览器的图标
- 如何创建ajax对象并兼容多个浏览器
- WinForm实现按名称递归查找控件的方法
- css ie6 ie7 ff的CSS hack使用技巧
- CSS 浏览器的等宽空格问题解决
- 区分IE6,IE7,firefox的CSS hack
- C#中父窗口和子窗口之间控件互操作实例
- Visual C++中MFC消息的分类
- MFC中Radio Button的用法详解