您现在的位置是:首页 > 技术文章网站首页技术文章

[CEF]同窗口打开新页面

  • WangYe
  • 2021-03-27 09:59:16
  • 249 次阅读
在做CEF嵌入Qt Demo时候遇到打开新页面链接不在同一窗口显示而是弹出新窗口,使用有了此文章;

效果:

找到 cef_life_span_handler.h -> OnBeforePopup

  /*--cef(optional_param=target_url,optional_param=target_frame_name)--*/
  virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             const CefString& target_url,
                             const CefString& target_frame_name,
                             WindowOpenDisposition target_disposition,
                             bool user_gesture,
                             const CefPopupFeatures& popupFeatures,
                             CefWindowInfo& windowInfo,
                             CefRefPtr<CefClient>& client,
                             CefBrowserSettings& settings,
                             CefRefPtr<CefDictionaryValue>& extra_info,
                             bool* no_javascript_access) {
    return false;
  }
  
改为

  /*--cef(optional_param=target_url,optional_param=target_frame_name)--*/
  virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             const CefString& target_url,
                             const CefString& target_frame_name,
                             WindowOpenDisposition target_disposition,
                             bool user_gesture,
                             const CefPopupFeatures& popupFeatures,
                             CefWindowInfo& windowInfo,
                             CefRefPtr<CefClient>& client,
                             CefBrowserSettings& settings,
                             CefRefPtr<CefDictionaryValue>& extra_info,
                             bool* no_javascript_access) {
	  switch (target_disposition)
	  {
	  case WOD_NEW_FOREGROUND_TAB:
	  case WOD_NEW_BACKGROUND_TAB:
	  case WOD_NEW_POPUP:
	  case WOD_NEW_WINDOW:
		  browser->GetMainFrame()->LoadURL(target_url);
		  return true; //停止创建
	  }
    return false;
  }

注释:

第一个参数browser代表了发出popup请求的浏览器对象,

frame是发出popup请求的那个frame,

target_url是要加载的目标url,

target_disposition是显示方式。

返回true就可以禁止创建新窗口。

TAG: CEF Vs Qt

文章评论 (0)



Top