Qt5.9.1结合CEF开发基于chorm的浏览器(二)

将libcef_dll_wrapper编译方式设置为MD

因为使用的Qt是动态链接的,而cef模式使用的是静态链接的方式,所以在使用前需要将cef编译方式改成Multi-thread DLL(/MD),修改路径在在C/C++->Code Generation下的Runtime Library。重新编译后的libcef_dll_wrapper.lib库大概26Mb

新建QtGUI项目

  为了快速实现,我们将使用cefsimple中的源码,将其嫁接到QtGUI中。

  首先把cef目录下的include拷贝到新项目中,再将libcef_dll_wrapper.liblibcef.dll拷贝到新项目的lib目录下。然后在项目中配置include和lib目录并将两个静态库添加到Linker->Input下。再将cefsimple中的simple_app.h、simple_app.cc、simple_handler.h、simple_handler.cc、simple_handler_win.cc拷贝到我们自己的项目源码目录下并在项目中添加。

新建.h和.cpp文件添加Cef初始化和退出函数

复制代码
 1 bool CefInit()
 2 {
 3     CefEnableHighDPISupport();
 4 
 5     CefSettings settings;
 6     settings.no_sandbox = true;
 7     settings.multi_threaded_message_loop = true;
 8 
 9     HINSTANCE inc = GetModuleHandle(NULL);
10     CefMainArgs mainArgs(inc);
11 
12     CefRefPtr<CefCommandLine> cmd_line = CefCommandLine::CreateCommandLine();
13     cmd_line->InitFromString(::GetCommandLineW());
14 
15     CefRefPtr<CefApp> app;
16     app = new SimpleApp;
17     return CefInitialize(mainArgs, settings, app.get(), NULL);
18 }
复制代码
1 void CefQuit()
2 {
3     CefShutdown();
4 }

在Qt的Gui类中添加初始化浏览器的方法

复制代码
 1 void QBrowser::InitBrowser()
 2 {
 3     CefWindowInfo cefWndInfo;
 4     QString strUrl = "http://baidu.com";
 5     HWND wnd = (HWND)ui.fmBrowser->winId();
 6 
 7     RECT winRect;
 8 
 9     QDesktopWidget* pDeskTop = QApplication::desktop();
10     QRect qtRect = pDeskTop->screenGeometry();
11     winRect.left = qtRect.left();
12     winRect.top = qtRect.top();
13     winRect.right = qtRect.right();
14     winRect.bottom = qtRect.bottom();
15 
16     cefWndInfo.SetAsChild(wnd, winRect);  //将cef界面嵌入qt界面中
17 
18     CefBrowserSettings cefBrowSetting;
19     m_browserEvent = CefRefPtr<SimpleHandler>(new SimpleHandler(true));
20     bool browser = CefBrowserHost::CreateBrowser(cefWndInfo, m_browserEvent, strUrl.toStdString(), cefBrowSetting, NULL);
21 
22     emit resize(qtRect.width(), qtRect.height());        //设置软件全屏
23 }
复制代码

为了响应程序窗口大小变化,重载ResizeEvent方法

复制代码
 1 void QBrowser::resizeEvent(QResizeEvent *event)
 2 {
 3     if (m_browserEvent.get() == NULL)
 4     {
 5         return;
 6     }
 7 
 8     QRect qtRect = ui.fmBrowser->rect();
 9     const BrowserList browList = m_browserEvent->GetBrowserList();
10 
11     if (!browList.empty())
12     {
13         HWND wnd = browList.front()->GetHost()->GetWindowHandle();
14         ::MoveWindow(wnd, qtRect.x(), qtRect.y(), qtRect.width(), qtRect.height(), true);
15     }
16 }
复制代码

记得在构造Gui类的时候调用InitBrowser方法!

最后在Main函数中进行Cef的初始化和销毁函数

复制代码
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    CefInit();

    QBrowser w;

    w.show();
    a.exec();

    CefQuit();

    return 0;
}
复制代码

然后可以编译运行了

  运行后发现,有两个窗口,因为simpleApp中也有一个初始化函数OnContextInitialized,我们在这个初始化函数开始位置进行reture即可。

扫描二维码关注公众号,回复: 943558 查看本文章

  万事开头难,有了这样一个小小的Demo之后我们就可以慢慢的分析cefsimple的实现,然后再将cef其他功能作用都添加到我们的项目中了。

猜你喜欢

转载自blog.csdn.net/fanhenghui/article/details/79781130
今日推荐