1、工具-NuGget-管理解决方案
2、在浏览中搜索cefsharp.winforms,选择第一个,并选择右下角的版本(我选择了47版本)
3、修改项目的平台目标为x86 解决方案-右键属性-选择配置属性-配置管理器-程序主项目选择x86
4、接下来可以开始写代码了,不详细写了,每个人要实现的功能不一样
5、这里涉及2个问题,一个是过滤图片,另一个是点击链接弹出窗口
5.1、解决过滤图片,需要继承IRequestHandler接口,并重写OnBeforeResourceLoad(这种方法在47以上版本无法使用)
public partial class requesthandler : IRequestHandler
{
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
callback.Dispose();
return false;
}
public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
return false;
}
/// <summary>
/// 过滤图片
/// </summary>
/// <returns></returns>
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if (request.ResourceType == ResourceType.Image)//过滤图片
return CefReturnValue.Cancel;
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
if (!callback.IsDisposed)
{
using (callback)
{
//To allow certificate
//callback.Continue(true);
//return true;
}
}
return false;
}
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{
//throw new NotImplementedException();
}
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
return false;
}
public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
if (!callback.IsDisposed)
{
using (callback)
{
//Accept Request to raise Quota
//callback.Continue(true);
//return true;
}
}
return false;
}
public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{
//throw new NotImplementedException();
}
public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{
//throw new NotImplementedException();
}
public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
//throw new NotImplementedException();
}
public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{
//throw new NotImplementedException();
}
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//throw new NotImplementedException();
return false;
}
}
5.2、点击链接弹出窗口,需要继承ILifeSpanHandler接口,并重写OnBeforePopup
/// <summary>
/// 在自己窗口打开链接
/// </summary>
public class OpenPageSelf : ILifeSpanHandler
{
public bool DoClose(IWebBrowser browserControl, IBrowser browser)
{
return false;
}
public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{
//throw new System.NotImplementedException();
}
public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
//throw new System.NotImplementedException();
}
public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IWindowInfo windowInfo, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
newBrowser = null;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
chromiumWebBrowser.Load(targetUrl);
return true; //Return true to cancel the popup creation copyright by codebye.com.
}
}
斌_789
发布了1 篇原创文章 · 获赞 0 · 访问量 15
私信
关注