栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

libcef-详细步骤-将cef浏览器嵌入到Win32中作为子窗口运行

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

libcef-详细步骤-将cef浏览器嵌入到Win32中作为子窗口运行

文章目录

1.新建Win32程序2.添加cefsimple中的文件3.取消预编译头文件4.添加包含文件和库文件

4.1.头文件夹4.2.库文件夹 5.添加代码

5.1.修改浏览器窗口位置5.2.目标位置加入代码 6.加入manifest文件7.作者答疑
  Win32程序是所有在windows系统上采用C++进行程序开发的系统子程序,是非常原生态的一种开发方式,大型软件一般会采用这种可以高度定制的方式来开发。在开发Win32程序时,面临需要嵌入浏览器的问题,可以采用嵌入libcef的方法来嵌入浏览器。本文测试环境采用VS2017+chromium-79.0.3945.88_windows64测试。

1.新建Win32程序

  在VS2017中新建Win32窗口项目,如下图所示:

2.添加cefsimple中的文件

  找到项目中对应文件,拷贝至Win32所在项目,如下图所示:

  将对应文件添加至解决方案,如下图所示:

3.取消预编译头文件

4.添加包含文件和库文件

  将包含文件和库路径添加至当前项目属性,头文件是项目文件夹下的include文件夹。

4.1.头文件夹

4.2.库文件夹

  本文将编译好的libcef_dll_wrapper.lib和debug对应其他资源文件整理在一个目录,如下所示:
![在这里插入图片描述](https://img-blog.csdnimg.cn/7d5bb5d6f23b46b0ad832ab213261e79.png#pic_center
  mfc项目添加库路径和库文件。如下图所示:
添加库文件:

libcef.lib
libcef_dll_wrapper.lib
opengl32.lib
comctl32.lib
rpcrt4.lib
shlwapi.lib
ws2_32.lib
d3d11.lib
glu32.lib
imm32.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib

5.添加代码 5.1.修改浏览器窗口位置

  将simple_app.cc文件中的以下内容去除,这部分内容是显示浏览器窗口使用。

void SimpleApp::OnContextInitialized() {
  CEF_REQUIRE_UI_THREAD();

  CefRefPtr command_line =
      CefCommandLine::GetGlobalCommandLine();

#if defined(OS_WIN) || defined(OS_LINUX)
  // Create the browser using the Views framework if "--use-views" is specified
  // via the command-line. Otherwise, create the browser using the native
  // platform framework. The Views framework is currently only supported on
  // Windows and Linux.
  const bool use_views = command_line->HasSwitch("use-views");
#else
  const bool use_views = false;
#endif

  // SimpleHandler implements browser-level callbacks.
  CefRefPtr handler(new SimpleHandler(use_views));

  // Specify CEF browser settings here.
  CefBrowserSettings browser_settings;

  std::string url;

  // Check if a "--url=" value was provided via the command-line. If so, use
  // that instead of the default URL.
  url = command_line->GetSwitchValue("url");
  if (url.empty())
    url = "http://www.baidu.com";

  if (use_views) {
    // Create the BrowserView.
    CefRefPtr browser_view = CefBrowserView::CreateBrowserView(
        handler, url, browser_settings, NULL, NULL, NULL);

    // Create the Window. It will show itself after creation.
    CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view));
  } else {
    // Information used when creating the native window.
    CefWindowInfo window_info;

#if defined(OS_WIN)
    // On Windows we need to specify certain flags that will be passed to
    // CreateWindowEx().
    window_info.SetAsPopup(NULL, "test simple cef");
#endif

    // Create the first browser window.
    CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
                                  NULL, NULL);
  }
}
5.2.目标位置加入代码

  在Win32中InitInstance函数内加入代码:

CefRefPtr g_handler;
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    //初始化
    //启用高分辨率,启动进程
    CefEnableHighDPISupport();
    void *sandbox_info = NULL;
    CefMainArgs main_args(hInstance);
    int exit_code = CefExecuteProcess(main_args, NULL, sandbox_info);
    if (exit_code >= 0)
    {
        return exit_code;
    }

    //关闭沙箱,浏览器初始化
    CefSettings settings;
    settings.no_sandbox = true;
    settings.multi_threaded_message_loop = true;
    CefRefPtr app(new SimpleApp);
    CefInitialize(main_args, settings, app.get(), sandbox_info);

    hInst = hInstance; // 将实例句柄存储在全局变量中

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    CefRefPtr _handler(new SimpleHandler(false));
    g_handler = _handler;
    CefWindowInfo window_info;
    RECT rect;
    GetWindowRect(hWnd, &rect);
    int w = rect.right - rect.left;
    int h = rect.bottom - rect.top;
    w = std::abs(w);
    h = std::abs(h);
    rect.left = 0;
    rect.right = w;
    rect.top = 0;
    rect.bottom = h;
    window_info.SetAsChild(hWnd, rect);
    //window_info.SetAsPopup(NULL, "hello");
    CefBrowserSettings browser_settings;
    CefBrowserHost::CreateBrowser(window_info, g_handler.get(), "http://www.baidu.com", browser_settings, NULL, NULL);

    return TRUE;
}

  在Win32中退出函数内加入代码:

CefShutdown();
6.加入manifest文件

  以上做完之后,在测试的时候发现浏览器显示空白,需要加入manifest文件,内容如下:


  
    
     
        
        
        
       
     
   

  在项目属性,清单工具,输入输出中,指定刚刚新建的cef.manifest文件。重新生成,即可显示目标网页。

7.作者答疑

  如有疑问,请留言。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779734.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号