当前位置:首页 > 在mfc中使用cef实现webkit的浏览器(三)-完成代码

在mfc中使用cef实现webkit的浏览器(三)-完成代码

点击次数:15216  更新日期:2014-11-27

前面,我们添加了一些cefsimple这个示例项目的代码到我们项目中。当然,这些代码需要修改一下,首先是#include 部分

#include 的路径需要调整一下,这里是删除"cefsimple/"。修改完#include后,编译一下,应该能够通过。

接下来,参考一下cefsimple里面“cefsimple_win.cpp”这个文件的代码,这里面的代码是初始化cef的

然后添加类似的代码到我们项目中

在我们项目的“MFCCef.cpp”这里面的构造函数中,添加如下代码


void* sandbox_info = NULL;

CefMainArgs main_args(AfxGetApp()->m_hInstance);

CefRefPtr<SimpleApp> app(new SimpleApp);

CefSettings settings;

settings.no_sandbox = true;

settings.multi_threaded_message_loop=true;

CefInitialize(main_args, settings, app.get(), sandbox_info);


如图:


将“simple_app.cpp”这个文件中的 void SimpleApp::OnContextInitialized() 这个方法里面的语句删除(这里面是创建窗口的方法,需要移到别的地方去)



在“MFCCefDlg.cpp”这个文件的OnInitDialog方法中,添加如下代码

CefWindowInfo window_info;

CRect rt;

GetWindowRect(&rt);

window_info.SetAsChild(this->GetSafeHwnd(), rt);

CefRefPtr<SimpleHandler> handler(new SimpleHandler());

CefBrowserSettings browser_settings;

std::string url;

url = "http://www.heycode.com";

CefBrowserHost::CreateBrowser(window_info, handler.get(), url, browser_settings, NULL);

同时添加这个类的析构函数,以便在程序关闭时,也关闭cef

方法体内容 CefShutdown();

注意:不要忘了添加头相应的文件哦

MFCCefDlg.cpp这里需要添加的头文件


#include "simple_handler.h"

#include "include/cef_browser.h"

#include "include/cef_command_line.h"

#include "include/wrapper/cef_helpers.h"

#include "simple_app.h"


MFCCef.cpp 需要添加的头文件

#include "simple_app.h"

#include "include/cef_sandbox_win.h"


运行一下,应该就能看到效果了