Cef sharp 使用心得【F12下载文件Loading动画Storage】

    技术2023-10-12  113

    Cef sharp把B/s包一层,感觉像是一个c/s客户端程序,其中支持:

    1. 捕获按键,做对应操作,包括F12,调出控制台调试;

    2. 添加缓存功能,指定缓存文件路径;

    3. 加载网页,等待时间用 Loading.gif动画代替;

    4. 支持下载文件,弹框选择路径;

    5. 隐藏鼠标右键,不弹无用菜单。

    Winform 代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using ADC; using CefSharp; using CefSharp.WinForms; namespace WindowsFormsApp { public partial class LoadIndex : Form { public LoadIndex() { InitializeComponent(); CalculationPosition(); var setting = new CefSettings { Locale = "zh-CN", AcceptLanguageList = "zh-CN,zh;q=0.8", PersistSessionCookies = true, UserAgent = "Mozilla/5.0 (Windows szl app @kaisen) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81", LocalesDirPath = System.IO.Path.GetFullPath("cache\\localeDir"), LogFile = System.IO.Path.GetFullPath("cache\\LogData"), UserDataPath = System.IO.Path.GetFullPath("cache\\UserData"), CachePath= System.IO.Path.GetFullPath("cache\\CacheFile") }; setting.CefCommandLineArgs.Add("disable-gpu", "1"); setting.CefCommandLineArgs.Add("--disable-web-security", "1"); setting.CefCommandLineArgs.Add("ppapi-flash-version", "18.0.0.209"); setting.CefCommandLineArgs.Add("ppapi-flash-path", "PepperFlash\\pepflashplayer.dll"); setting.CefCommandLineArgs.Add("--enable-system-flash", "1"); CefSharpSettings.LegacyJavascriptBindingEnabled = true; CefSharp.Cef.Initialize(setting); var webview = new ChromiumWebBrowser(ConfigurationManager.AppSettings["LoadIndexUrl"].ToString()); webview.Dock = DockStyle.Fill; webview.MenuHandler = new MenuHandler(); webview.KeyboardHandler = new CEFKeyBoardHander(); webview.DownloadHandler = new DownLoadFile(); this.Controls.Add(webview); webview.FrameLoadStart += new EventHandler<FrameLoadStartEventArgs>(Browser_FrameLoadStart); webview.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameEndFunc); this.Text = string.Format(" {0} V 1.1", ConfigurationManager.AppSettings["LoadIndexTitle"].ToString()); this.WindowState = FormWindowState.Maximized; } #region Loading private void CalculationPosition() { Rectangle rect = Screen.GetWorkingArea(this); lab_loading.Location = new Point(0,0); lab_loading.Size = new Size(rect.Width, rect.Height); } private void Browser_FrameLoadStart(object sender, FrameLoadStartEventArgs e) { Loading_Show(); } private delegate void dlgLoading_Show(); private void Loading_Show() { if (lab_loading.InvokeRequired) { dlgLoading_Show dlShow = new dlgLoading_Show(Loading_Show); lab_loading.Invoke(dlShow,null); } else { this.lab_loading.Visible = true; } } private delegate void dlgLoading_Hide(); private void Loading_Hide() { if (lab_loading.InvokeRequired) { dlgLoading_Show dlHide = new dlgLoading_Show(Loading_Hide); lab_loading.Invoke(dlHide, null); } else { Thread.Sleep(500);//等待cef渲染控件,再去掉Loading动画 this.lab_loading.Visible = false; } } private void FrameEndFunc(object sender, FrameLoadEndEventArgs e) { Loading_Hide(); } #endregion } }

    运行效果:

    源码下载:https://download.csdn.net/download/ybb350680013/12573380

    Processed: 0.050, SQL: 9