CefSharp 能 在C# winform 中实现 浏览器 功能
同样的在浏览器中 网页上如果有 下载 按钮 实现下载功能
直接上代码
CefSharp 在winform上实现 浏览器功能
using System; using System.Text; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms;
public ChromiumWebBrowser browser; public void InitBrowser() { //Cef.Initialize(new CefSettings()); browser = new ChromiumWebBrowser("https://ai.12348.gov.cn/pc/"); browser.LifeSpanHandler = new LifeSpanHandler();//超链接不弹新窗体 browser.DownloadHandler = new DownloadHandler();//下载 this.panel1.Controls.Add(browser);//panel }
重点: 创建CustomDownloadHandler类并实现IDownloadHandler接口
创建 DownloadHandler.cs 文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; //using System.Web.Script.Serialization; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; using System.IO; namespace LawSever { public class DownloadHandler : IDownloadHandler { public event EventHandler<DownloadItem> OnBeforeDownloadFired; public event EventHandler<DownloadItem> OnDownloadUpdatedFired; public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { //throw new NotImplementedException(); var handler = OnBeforeDownloadFired; if (handler != null) { handler(this, downloadItem); } if (!callback.IsDisposed) { //using (callback) //{ // callback.Continue(downloadItem.SuggestedFileName, showDialog: true); //} using (callback) {
//好用 downloadItem.SuggestedFileName为下载的文件名 //callback.Continue(@"C:\Users\" + // System.Security.Principal.WindowsIdentity.GetCurrent().Name + @"\Downloads\" + // downloadItem.SuggestedFileName, // showDialog: false); string strDownloadFile = @"c:\DownloadFile\file.docx";//手动指定下载目录和下载文件名 callback.Continue(@strDownloadFile,showDialog: false); } } } public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { //下载过程中 调用多次 } public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { //没有被调用 } public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { //没有被调用 //var handler = OnDownloadUpdatedFired; //if (handler != null) //{ // handler(this, downloadItem); //} }
} }
超链接不弹新窗体
创建 LifeSpanHandler.cs 文件
using CefSharp; using CefSharp.WinForms; namespace LawSever { internal class LifeSpanHandler : ILifeSpanHandler { //弹出前触发的事件 public bool OnBeforePopup(IWebBrowser webBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { //使用源窗口打开链接,取消创建新窗口 newBrowser = null; var chromiumWebBrowser = (ChromiumWebBrowser)webBrowser; chromiumWebBrowser.Load(targetUrl); return true; } public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser) { } public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { return true; } public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { } } }
感谢:
https://www.javaroad.cn/questions/288431
https://www.cnblogs.com/mchao/p/13935777.html
标签:CefSharp,功能,System,callback,using,public,下载,browser From: https://www.cnblogs.com/hailexuexi/p/16935850.html