第二步:QT窗口内嵌CEF

 

libcef_dll_wrapper 项目设置c/c++ ->代码生成->运行库选择  debug为/MDd  release 为 /MD ,生成。

 

用qtcreater 新建一个项目qtbrowser,为了方便,在vs2013里打开qtbrowser.pro,链接库选择libcef_dll_wrapper.lib,libcef.lib

头文件包含 cef目录的 /include .

然后把cefsimple 下的几个代码文件复制到qtbrowser下,全部添加。

接下来就像这样:

bool QcefCustom::CefInit()
{
    CefEnableHighDPISupport();

    CefSettings settings;
    settings.no_sandbox = true;
    settings.multi_threaded_message_loop = true;
    HINSTANCE inc = GetModuleHandle(NULL);
    CefMainArgs mainArgs(inc);

    CefRefPtr<CefCommandLine> cmd_line = CefCommandLine::CreateCommandLine();
    cmd_line->InitFromString(::GetCommandLineW());
    CefString t = cmd_line->GetCommandLineString();
    CefRefPtr<CefApp> app;
    app = new SimpleApp();
    CefExecuteProcess(mainArgs, app, NULL);
    CefInitialize(mainArgs, settings, app.get(), NULL);

    return true;
}

 

 

void QcefCustom::InitBrowser()
{
    QString strUrl =  "www.baidu.com";
    CefRefPtr<SimpleHandler> handler(new SimpleHandler(false));
    m_browserEvent = SimpleHandler::GetInstance();
    CefWindowInfo cefWndInfo;

    HWND wnd = (HWND)this->winId();
    RECT winRect;
    winRect.left = 0;
    winRect.top = 0;
    winRect.right = DEF_WIGTH;
    winRect.bottom = DEF_HEIHT;

    cefWndInfo.SetAsChild(wnd, winRect);
    CefBrowserSettings cefBrowSetting;
    CefBrowserHost::CreateBrowser(cefWndInfo, handler, strUrl.toStdString(), cefBrowSetting, NULL);
}

先CefInit(),再调用 InitBrowser();

然后用vs2013编译,生成了exe之后,把cefsimple/bin 下的所有依赖文件考过来,注意:用新生成的libcef_dll_wrapper.dll libcef.dll替换之前的,然后运行起来就可以了。

效果如下:

QT 内嵌CEF(2)

 

滚动条怎么加的?

qt窗口继承 :void QcefCustom::resizeEvent(QResizeEvent* pResizeEvent)

然后外窗口变化,同时也MoveWindow 内部的浏览器窗口,就可以了。

debug下运行白屏不知道为什么,退出崩溃的问题release下没有出现。

相关文章: