Cef开发学习 – Windows平台简易的Cef浏览器,支持下载管理和多标签管理
Cef开发学习 – Windows平台简易的Cef浏览器,支持下载管理和多标签管理
PS:本系列文章主要学习介绍Cef开发相关的内容。
一、浏览器内核
本程序使用的Cef开发内核库的版本为libcef 3809,重新编译的后的3809库支持MP4视频播放。
class ICefBrowserHandler
{
public:
virtual void OnLoadEnd(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode){}
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) { }
virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& target_url,
const CefString& target_frame_name,
cef_window_open_disposition_t target_disposition,
bool user_gesture,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
CefRefPtr<CefDictionaryValue>& extra_info,
bool* no_javascript_access) { return true; }
///
// Called when a frame's address has changed.
///
/*--cef()--*/
virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& url) {}
///
// Called when the page title changes.
///
/*--cef(optional_param=title)--*/
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {}
///
// Called when the page icon changes.
///
/*--cef(optional_param=icon_urls)--*/
virtual void OnFaviconURLChange(CefRefPtr<CefBrowser> browser,
const std::vector<CefString>& icon_urls) {}
///
// Called when web content in the page has toggled fullscreen mode. If
// |fullscreen| is true the content will automatically be sized to fill the
// browser content area. If |fullscreen| is false the content will
// automatically return to its original size and position. The client is
// responsible for resizing the browser if desired.
///
/*--cef()--*/
virtual void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
bool fullscreen) {}
///
// Called when the browser is about to display a tooltip. |text| contains the
// text that will be displayed in the tooltip. To handle the display of the
// tooltip yourself return true. Otherwise, you can optionally modify |text|
// and then return false to allow the browser to display the tooltip.
// When window rendering is disabled the application is responsible for
// drawing tooltips and the return value is ignored.
///
/*--cef(optional_param=text)--*/
virtual bool OnTooltip(CefRefPtr<CefBrowser> browser,
CefString& text) { return false; }
///
// Called when the browser receives a status message. |value| contains the
// text that will be displayed in the status message.
///
/*--cef(optional_param=value)--*/
virtual void OnStatusMessage(CefRefPtr<CefBrowser> browser,
const CefString& value) {}
///
// Called to display a console message. Return true to stop the message from
// being output to the console.
///
/*--cef(optional_param=message,optional_param=source)--*/
virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
const CefString& message,
const CefString& source,
int line) { return false; }
};
二、浏览器UI
本程序使用开源的Duilib库作为浏览器的UI开发库,实现了浏览器多窗口、多标签、下载管理的各个模块。 1、Cef浏览器控件
class CCefBrowserUI : public CControlUI, public IMessageFilterUI
{
public:
CCefBrowserUI();
~CCefBrowserUI();
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
public:
void SetPos(RECT rc, bool bNeedInvalidate = true);
void SetVisible(bool bVisible /* = true */);
void SetInternVisible(bool bVisible);
void DoEvent(TEventUI& event);
}
2、多标签管理
class CBrowserTabBar : public CContainerUI
{
public:
CBrowserTabBar();
~CBrowserTabBar();
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
void SetPos(RECT rc, bool bNeedInvalidate = true);
public:
bool Add(CControlUI* pControl);
bool AddAt(CControlUI* pControl, int iIndex);
void Invalidate();
void SetStartTab(int nStart);
void SelectTab(CBrowserTab *pTab);
void SelectTab(int nIndex);
void CloseTab(CBrowserTab *pTab, BOOL bPrevSelected = TRUE);
int GetTabCount();
int GetVisibleTabCount();
CBrowserTab* GetTab(int nIndex);
int GetTabIndex(CBrowserTab *pTab);
CBrowserTab* GetPrevTab(CBrowserTab *pTab);
CBrowserTab* GetNextTab(CBrowserTab *pTab);
private:
void UpdatePostPaint(CBrowserTab *pTab);
private:
int m_nStartTab;
int m_nSelectedTab;
};
三、浏览器演示
https://live.csdn.net/v/embed/216646
四、浏览器例子下载
总结
随着桌面软件开发的移动化和Web化,集成Cef后,极大的提高了前端复杂项目的开发效率,核心代码一次编译,多平台共享使用!