天天看點

Cef sharp 使用心得【F12/下載下傳檔案/Loading動畫/Storage】

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
    }
}
           

運作效果:

Cef sharp 使用心得【F12/下載下傳檔案/Loading動畫/Storage】
Cef sharp 使用心得【F12/下載下傳檔案/Loading動畫/Storage】

源碼下載下傳:https://download.csdn.net/download/ybb350680013/12573380