using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; using System.Linq; using System.Net; using CefSharp; using CefSharp.Enums; using CefSharp.Structs; using Size = CefSharp.Structs.Size; namespace XeroBrowser { public class DisplayHandler : IDisplayHandler { private readonly FrmBrowser _frmref; public DisplayHandler(FrmBrowser frm) { _frmref = frm; } private static Icon BytesToIcon(byte[] bytes) { using (MemoryStream ms = new MemoryStream(bytes)) { return new Icon(ms); } } public void OnAddressChanged(IWebBrowser chromiumWebBrowser, AddressChangedEventArgs addressChangedArgs) { } public bool OnAutoResize(IWebBrowser chromiumWebBrowser, IBrowser browser, Size newSize) { return true; } public bool OnConsoleMessage(IWebBrowser chromiumWebBrowser, ConsoleMessageEventArgs consoleMessageArgs) { return true; } public bool OnCursorChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr cursor, CursorType type, CursorInfo customCursorInfo) { return true; } public void OnFaviconUrlChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IList urls) { try { if (urls.Count > 0) { WebClient wc = new WebClient(); byte[] buffer = wc.DownloadData(urls[0]); _frmref.Icon = BytesToIcon(buffer); _frmref.Update(); _frmref.ParentTabs.UpdateThumbnailPreviewIcon(_frmref.ParentTabs.Tabs.Single(t => t.Content == _frmref)); _frmref.ParentTabs.RedrawTabs(); } } catch (Exception){ try { WebClient wc = new WebClient(); Uri furi = new Uri(chromiumWebBrowser.Address); byte[] buffer = wc.DownloadData(furi.GetLeftPart(UriPartial.Authority) +"/favicon.ico"); _frmref.Icon = BytesToIcon(buffer); _frmref.Update(); _frmref.ParentTabs.UpdateThumbnailPreviewIcon(_frmref.ParentTabs.Tabs.Single(t => t.Content == _frmref)); _frmref.ParentTabs.RedrawTabs(); } catch (Exception){ ComponentResourceManager resources = new ComponentResourceManager(typeof(FrmBrowser)); _frmref.Icon = ((Icon)(resources.GetObject("$this.Icon"))); _frmref.Update(); _frmref.ParentTabs.UpdateThumbnailPreviewIcon(_frmref.ParentTabs.Tabs.Single(t => t.Content == _frmref)); _frmref.ParentTabs.RedrawTabs(); } } } public void OnFullscreenModeChange(IWebBrowser chromiumWebBrowser, IBrowser browser, bool fullscreen) { } public void OnLoadingProgressChange(IWebBrowser chromiumWebBrowser, IBrowser browser, double progress) { } public void OnStatusMessage(IWebBrowser chromiumWebBrowser, StatusMessageEventArgs statusMessageArgs) { } public void OnTitleChanged(IWebBrowser chromiumWebBrowser, TitleChangedEventArgs titleChangedArgs) { } public bool OnTooltipChanged(IWebBrowser chromiumWebBrowser, ref string text) { return true; } } }