Cef开发学习 - Windows平台简易的Cef浏览器,支持下载管理和多标签管理

本文介绍了如何在Windows平台上利用Cef库开发一个支持下载管理和多标签功能的浏览器。通过CefBrowserHandler类实现内核交互,利用Duilib库创建用户界面,提供浏览器控件和标签栏的管理。集成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;
};

三、浏览器演示

Cef浏览器

四、浏览器例子下载

Cef浏览器下载

总结

随着桌面软件开发的移动化和Web化,集成Cef后,极大的提高了前端复杂项目的开发效率,核心代码一次编译,多平台共享使用!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值