MFC对话框应用程序中谷歌CEF浏览器内核的使用

来源:互联网 发布:centos 6.5 计划任务 编辑:程序博客网 时间:2024/05/29 09:27

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);
                   }
         }
}

 

0 0
原创粉丝点击