您当前的位置: 首页 > 

插件开发

暂无认证

  • 1浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

libcef-案例展示-将cef浏览器嵌入到mfc中作为子窗口运行

插件开发 发布时间:2022-03-21 10:42:25 ,浏览量:1

文章目录
    • 1.新建MFC项目
    • 2.添加cefsimple中的文件
    • 3.取消预编译头文件
    • 4.添加包含文件和库文件
      • 4.1 头文件夹
      • 4.2 库文件夹
    • 5.修改部分错误
      • 5.1 编译错误
    • 6.添加代码
      • 6.1.修改浏览器窗口位置
      • 6.2.目标位置加入代码
    • 7.加入manifest文件
    • 8.最终效果
    • 9.作者答疑
  MFC作为传统的C++框架,在许多现有的windows应用程序中,还是大量存在,在如今的互联网世界里,web已经成为了不可或缺的一环,通过html5展示页面是一种非常方便而有效的技术。这样,将浏览器作为一个子窗口嵌入到mfc中,有大量的需求存在。本文展示如何将cef浏览器嵌入mfc中运行,并打开一个网页。本文测试环境采用VS2017+chromium-79.0.3945.88_windows64测试。

1.新建MFC项目

  在VS2017中新建MFC对话框项目,如下图所示: 在这里插入图片描述

2.添加cefsimple中的文件

  找到项目中对应文件,拷贝至MFC所在项目,如下图所示: 在这里插入图片描述

  将对应文件添加至解决方案,如下图所示: 在这里插入图片描述

3.取消预编译头文件

在这里插入图片描述

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

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

4.1 头文件夹

在这里插入图片描述 在这里插入图片描述

4.2 库文件夹

  本文将编译好的libcef_dll_wrapper.lib和debug对应其他资源文件整理在一个目录,如下所示: 在这里插入图片描述  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 编译错误
bool ReadFileToString(const std::string& path,
                      std::string* contents,
                      size_t max_size = std::numeric_limits::max());
//warning C4003: 类函数宏的调用“max”参数不足
//改为--添加括号
bool ReadFileToString(const std::string& path,
                      std::string* contents,
                      size_t max_size = (std::numeric_limits::max)());

      int transfer_size =
          std::min(bytes_to_read, static_cast(data_.length() - offset_));
//error C2589: “(”:“::”右边的非法标记
//改为--添加括号
        int transfer_size =
          (std::min)(bytes_to_read, static_cast(data_.length() - offset_));   

DCHECK_EQ((std::max)(image->GetWidth(), image->GetHeight()), 16U);//添加括号  
6.添加代码 6.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);
  }
}
6.2.目标位置加入代码

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

BOOL CMFCCefApp::InitInstance()
{	/*m_cefApp = new client::ClientAppBrowser();
	if (!(m_cefApp->Init(m_hInstance) < 0))
		return FALSE;*/

	//参数初始化
	CefMainArgs main_args(m_hInstance);
	CefRefPtr app(new SimpleApp);
	int exit_code = CefExecuteProcess(main_args, app.get(), NULL);
	if (exit_code >= 0) {
		// The sub-process has completed so return here.
		return FALSE;
	}
	CefSettings settings;
	settings.no_sandbox = true;
	settings.multi_threaded_message_loop = true;	
	CefInitialize(main_args, settings, app.get(), NULL);

	// 如果一个运行在 Windows XP 上的应用程序清单指定要
	// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
	//则需要 InitCommonControlsEx()。  否则,将无法创建窗口。
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// 将它设置为包括所有要在应用程序中使用的
	// 公共控件类。
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// 创建 shell 管理器,以防对话框包含
	// 任何 shell 树视图控件或 shell 列表视图控件。
	CShellManager *pShellManager = new CShellManager;

	// 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小,则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串,
	// 例如修改为公司或组织名
	SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

	CMFCCefDlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: 在此放置处理何时用
		//  “确定”来关闭对话框的代码
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: 在此放置处理何时用
		//  “取消”来关闭对话框的代码
	}
	else if (nResponse == -1)
	{
		TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n");
		TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
	}

	// 删除上面创建的 shell 管理器。
	if (pShellManager != nullptr)
	{
		delete pShellManager;
	}

#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
	ControlBarCleanUp();
#endif

	// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
	//  而不是启动应用程序的消息泵。
	return FALSE;
}

  在退出函数中加入代码:

int CMFCCefApp::ExitInstance()
{
	// TODO: 在此添加专用代码和/或调用基类
	CefShutdown();
	return CWinApp::ExitInstance();
}

  在窗口显示中加入代码:

CefRefPtr g_handler;
BOOL CMFCCefDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	CefRefPtr _handler(new SimpleHandler(false));
	g_handler = _handler;
	CefWindowInfo window_info;
	CRect rect;
	GetWindowRect(&rect);
	window_info.SetAsChild(m_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;  // 除非将焦点设置到控件,否则返回 TRUE
}

  在窗口大小变动消息中加入代码:

void CMFCCefDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	if (g_handler.get())
	{
		CefWindowHandle hwnd = g_handler->GetBrowser()->GetHost()->GetWindowHandle();
		CRect rc;
		GetClientRect(rc);
		::MoveWindow(hwnd, rc.left, rc.top, rc.Width(), rc.Height(), true);
	}
}
7.加入manifest文件

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


  
    
     
        
        
        
       
     
   

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

8.最终效果

在这里插入图片描述

9.作者答疑

  如有疑问,请留言。

关注
打赏
1665481431
查看更多评论
立即登录/注册

微信扫码登录

0.0396s