原文:cefsharp 与webbrowser简单对比概述

有个项目需要做个简单浏览器,从网上了解到几个相关的组件有winform自带的IE内核的WebBrowser,有第三方组件谷歌内核的webkit、cefsharp、chromiumfx等。

一开始是用的webbrowser 后来发现有些css样式是不兼容的于是又开始研究webkit这个有些css样式同样不能用,然后才开始研究cefsharp,从网上下载了57版本的但总是运行不起来后来发现装上vs中C++公共工具才行(注意即使装上vcredist_x86这个运行库也是不行),这个你不能要求人家客户装vs啊,后来也没找到到底是哪个动态库的问题。之后就下载了47版本。

按键事件

   不同于webbrowser通过事件PreviewKeyDown捕获键盘按键,cefsharp是通过实现接口IKeyboardHandler赋值委托KeyboardHandler来实现的

 web.KeyboardHandler = new CefKeyboardHandler(m_cell as CellControl);
 public class CefKeyboardHandler : IKeyboardHandler
{
private CellControl m_cell; /// <summary>
/// 命名代理
/// </summary>
/// <param name="con"></param>
/// <param name="text"></param>
private delegate void PreKeyEventHandler(object sender,object webBrowser,int windowsKeyCode); public CefKeyboardHandler(CellControl cell)
{
m_cell = cell;
}
public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
return true;
}
public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
{
m_cell.Invoke(new PreKeyEventHandler(PreKeyEvent),m_cell, browserControl, windowsKeyCode);
return true;
} private void PreKeyEvent(object sender, object webBrowser, int windowsKeyCode)
{
CellControl cell = sender as CellControl;
cell.ControlPreviewKeyDown(webBrowser , new System.Windows.Forms.PreviewKeyDownEventArgs((Keys)windowsKeyCode));
if (windowsKeyCode == (int)(Keys.Delete))
{
cell.Controls.Clear();
} }

代理设置

   公司网络是需要代理才能上外网,webbrowser可以自动获取已经设置好的代理,cefsharp网上说可以通过setting.CefCommandLineArgs.Add("--proxy-server", "http://127.0.0.1:8877");这个方式设置代理,可是不知道为啥我这边一直不行,后来发现一个通过实现接口IRequestHandler赋值委托RequestHandler来设置代理可行

  webBrowser.RequestHandler = new CefRequest();
 public class CefRequest : IRequestHandler
{
//
// 摘要:
// Called when the browser needs credentials from the user.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame object that needs credentials (This will contain the URL that is being
// requested.)
//
// isProxy:
// indicates whether the host is a proxy server
//
// host:
// hostname
//
// port:
// port number
//
// realm:
// realm
//
// scheme:
// scheme
//
// callback:
// Callback interface used for asynchronous continuation of authentication requests.
//
// 返回结果:
// Return true to continue the request and call CefAuthCallback::Continue() when
// the authentication information is available. Return false to cancel the request.
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
if(isProxy)
{
if (string.IsNullOrWhiteSpace(ProxyThornInfo.Name))
{
ProxyThornInfo.InitInfo(ProxyThornInfo.DefaultFile);
}
}
//callback.Cancel();
callback.Continue(ProxyThornInfo.Name, ProxyThornInfo.PassWord);
return true;
} //
// 摘要:
// Called before browser navigation. If the navigation is allowed CefSharp.IWebBrowser.FrameLoadStart
// and CefSharp.IWebBrowser.FrameLoadEnd will be called. If the navigation is canceled
// CefSharp.IWebBrowser.LoadError will be called with an ErrorCode value of CefSharp.CefErrorCode.Aborted.
//
// 参数:
// browserControl:
// the ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame the request is coming from
//
// request:
// the request object - cannot be modified in this callback
//
// isRedirect:
// has the request been redirected
//
// 返回结果:
// Return true to cancel the navigation or false to allow the navigation to proceed.
public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
return false;
}
//
// 摘要:
// Called before a resource request is loaded. For async processing return CefSharp.CefReturnValue.ContinueAsync
// and execute CefSharp.IRequestCallback.Continue(System.Boolean) or CefSharp.IRequestCallback.Cancel
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame object
//
// request:
// the request object - can be modified in this callback.
//
// callback:
// Callback interface used for asynchronous continuation of url requests.
//
// 返回结果:
// To cancel loading of the resource return CefSharp.CefReturnValue.Cancel or CefSharp.CefReturnValue.Continue
// to allow the resource to load normally. For async return CefSharp.CefReturnValue.ContinueAsync
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return CefReturnValue.Continue;
} //
// 摘要:
// Called on the CEF IO thread when a resource response is received. To allow the
// resource to load normally return false. To redirect or retry the resource modify
// request (url, headers or post body) and return true. The response object cannot
// be modified in this callback.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame that is being redirected.
//
// request:
// the request object
//
// response:
// the response object - cannot be modified in this callback
//
// 返回结果:
// To allow the resource to load normally return false. To redirect or retry the
// resource modify request (url, headers or post body) and return true.
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
} //
// 摘要:
// Called to handle requests for URLs with an invalid SSL certificate. Return true
// and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method
// or at a later time to continue or cancel the request. If CefSettings.IgnoreCertificateErrors
// is set all invalid certificates will be accepted without calling this method.
//
// 参数:
// browserControl:
// the ChromiumWebBrowser control
//
// browser:
// the browser object
//
// errorCode:
// the error code for this invalid certificate
//
// requestUrl:
// the url of the request for the invalid certificate
//
// sslInfo:
// ssl certificate information
//
// callback:
// Callback interface used for asynchronous continuation of url requests. If empty
// the error cannot be recovered from and the request will be canceled automatically.
//
// 返回结果:
// Return false to cancel the request immediately. Return true and use CefSharp.IRequestCallback
// to execute in an async fashion.
public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return true;
} //
// 摘要:
// Called on the UI thread before OnBeforeBrowse in certain limited cases where
// navigating a new or different browser might be desirable. This includes user-initiated
// navigation that might open in a special way (e.g. links clicked via middle-click
// or ctrl + left-click) and certain types of cross-origin navigation initiated
// from the renderer process (e.g. navigating the top-level frame to/from a file
// URL).
//
// 参数:
// browserControl:
// the ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame object
//
// targetUrl:
// target url
//
// targetDisposition:
// The value indicates where the user intended to navigate the browser based on
// standard Chromium behaviors (e.g. current tab, new tab, etc).
//
// userGesture:
// The value will be true if the browser navigated via explicit user gesture (e.g.
// clicking a link) or false if it navigated automatically (e.g. via the DomContentLoaded
// event).
//
// 返回结果:
// Return true to cancel the navigation or false to allow the navigation to proceed
// in the source browser's top-level frame.
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
} //
// 摘要:
// Called when a plugin has crashed
//
// 参数:
// browserControl:
// the ChromiumWebBrowser control
//
// browser:
// the browser object
//
// pluginPath:
// path of the plugin that crashed
public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{ } //
// 摘要:
// Called on the UI thread to handle requests for URLs with an unknown protocol
// component. SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS
// BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// url:
// the request url
//
// 返回结果:
// return to true to attempt execution via the registered OS protocol handler, if
// any. Otherwise return false.
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
return true;
}
//
// 摘要:
// Called when JavaScript requests a specific storage quota size via the webkitStorageInfo.requestQuota
// function. For async processing return true and execute CefSharp.IRequestCallback.Continue(System.Boolean)
// at a later time to grant or deny the request or CefSharp.IRequestCallback.Cancel
// to cancel.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// originUrl:
// the origin of the page making the request
//
// newSize:
// is the requested quota size in bytes
//
// callback:
// Callback interface used for asynchronous continuation of url requests.
//
// 返回结果:
// Return false to cancel the request immediately. Return true to continue the request
// and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method
// or at a later time to grant or deny the request.
public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return true;
}
//
// 摘要:
// Called when the render process terminates unexpectedly.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// status:
// indicates how the process terminated.
public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{ }
//
// 摘要:
// Called on the browser process UI thread when the render view associated with
// browser is ready to receive/handle IPC messages in the render process.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{ }
//
// 摘要:
// Called on the CEF IO thread when a resource load has completed.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame that is being redirected.
//
// request:
// the request object - cannot be modified in this callback
//
// response:
// the response object - cannot be modified in this callback
//
// status:
// indicates the load completion status
//
// receivedContentLength:
// is the number of response bytes actually read.
public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{ }
//
// 摘要:
// Called on the IO thread when a resource load is redirected. The CefSharp.IRequest.Url
// parameter will contain the old URL and other request-related information.
//
// 参数:
// browserControl:
// The ChromiumWebBrowser control
//
// browser:
// the browser object
//
// frame:
// The frame that is being redirected.
//
// request:
// the request object - cannot be modified in this callback
//
// newUrl:
// the new URL and can be changed if desired
public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{ }

其他的ChromiumWebBrowser.Handler同上

JavaScript互调

   
在webbrowser中通过设置  

[System.Runtime.InteropServices.ComVisible(true)]

public class JavaScripAction

webBrowser1.ObjectForScripting
= new JavaScripAction();
就可在javascrip中通过window.external来调用C#类JavaScriptAction中的方法,在C#中通过来调用javascrip中的函数

web.RegisterJsObject("objbine", java, false);这里的objbine是我们自己起的这个注册的名字实体,注意不能和java中特殊含义的文字重复例如external否则没有效果


webBrowser1.Document.InvokeScript("Messageaa",
objects);

在cefsharp中是通过注册来实现java与C#类的绑定的

JavaScripAction java = new JavaScripAction(dataSvc, bbShow, pdpcShowBb);

这样在javascrip中可以通过

window.objbine.InvokeFunc('0','1','cunction','1,2,3');

这种方式来调用C#类JavaScriptaction中的方法InvokFunc方法了,需要注意的是如果

在C#中调用JavaScript中的函数如下

web.ExecuteScriptAsync(string methodname,params object[] args)

需要注意的是如果JavaScript中的函数名是没有参数的这里的methodname需要在函数名后面加上括号的如果没有参数就不能加括号。

cefsharp 与webbrowser简单对比概述的更多相关文章

  1. MongoDB中insert方法、update方法、save方法简单对比

    MongoDB中insert方法.update方法.save方法简单对比 1.update方法 该方法用于更新数据,是对文档中的数据进行更新,改变则更新,没改变则不变. 2.insert方法 该方法用 ...

  2. .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比

    在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy的优势在哪里?和微软的MVC比 ...

  3. HTTPS, SPDY和 HTTP/2性能的简单对比

    中文原文:HTTPS, SPDY和 HTTP/2性能的简单对比 整理自:A Simple Performance Comparison of HTTPS, SPDY and HTTP/2 请尊重版权, ...

  4. 【转贴】Cortex系列M0-4简单对比

    转载网址:http://blog.sina.com.cn/s/blog_7dbd9c0e01018e4l.html 最近搞了块ST的Cortex-M4处理器,然后下了本文档.分享一下. 针对目前进入大 ...

  5. 简单对比Spark和Storm

    2013年参与开发了一个类似storm的自研系统, 2014年使用过spark 4个多月,对这两个系统都有一些了解. 下面是我关于这两个系统的简单对比: Spark: 1. 基于数据并行,https: ...

  6. Nancy和MVC的简单对比

    Nancy和MVC的简单对比 在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy ...

  7. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)

    [评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...

  8. 百度 OCR API 的使用以及与 Tesseract 的简单对比

    目录 百度 OCR API 初探 用 Python 调用百度 OCR API 与 Tesseract 的简单对比 百度 OCR API 初探 近日得知百度在其 APIStore 上开放了 OCR 的 ...

  9. Rx与Async Task的简单对比

    有关Reactive Extensions的介绍可见https://rx.codeplex.com/,总的来说,你可以当它是又一个异步编程的框架,它以观察者模式实现了对数据流的的“订阅”.一个列表,一 ...

随机推荐

  1. java连接MongoDB查询导出为excel表格

    背景 因为项目需求.每一个星期须要统计每一个公众号7天的訪问数,月底就须要统计一个月的訪问数,40多个公众号.每次手动统计都须要花费1个小时,总之是一项无技术耗时耗神的手工活. 于是.想写个程序来统计 ...

  2. 【34.25%】【BZOJ 2648】SJY摆棋子

    Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 2718  Solved: 931 [Submit][Status][Discuss] Descrip ...

  3. jquery pagination分页的两种实现方式

    原文链接:http://blog.csdn.net/qq_37936542/article/details/79457012 此插件是jQuery的ajax分页插件.如果你用到此插件作分页的时候,涉及 ...

  4. C++开发人脸性别识别教程(5)——通过FaceRecognizer类实现性别识别

    在之前的博客中已经攻克了人脸检測的问题,我们计划在这篇博客中介绍人脸识别.性别识别方面的相关实现方法. 事实上性别识别和人脸识别本质上是相似的,由于这里仅仅是一个简单的MFC开发,主要工作并不在算法研 ...

  5. 【codeforces 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 在视图上创建ListCtrl的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 今天介绍下如何在一个视图上动态创建一个ListCtrl. 1.新建一个MFC的单文档工程,这里暂定名字为ListDem ...

  7. git撤销操作总结

    git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash pop回复. git rese ...

  8. ASP.NET中前台如何调用后台变量

    .Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %> ...

  9. Bean行为破坏之前,

    而在自定义初始化阶段的行为之一似.Spring此外,它提供了两种方法来定制Bean具体的行为破坏之前. 例如下列: 1.采用destroy-method属性. 2.达到DisposableBean介面 ...

  10. VC++实现Vista和Win7系统低权限程序向高权限程序发消息

    Windows 7已经隆重发布,但是很多程序员已经通过RTM等版本尝到了Windows 7的甜处.那么在Windows 7下用户界面特权隔离,将是本文我们介绍的重点. 我们介绍了操作系统服务的Sess ...