C# 用更好的浏览器(如 Chrome)替换 .NET WebBrowser 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/790542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:15:50  来源:igfitidea点击:

Replacing .NET WebBrowser control with a better browser, like Chrome?

c#.netwinformswebbrowser-control

提问by Sylverdrag

Is there any relatively easy way to insert a modern browser into a .NET application?

有没有相对简单的方将现代浏览器插入 .NET 应用程序?

As far as I understand, the WebBrowsercontrol is a wrapper for IE, which wouldn't be a problem except that it looks like it is a very old version of IE, with all that entails in terms of CSS screw-ups, potential security risks (if the rendering engine wasn't patched, can I really expect the zillion buffer overflow problems to be fixed?), and other issues.

据我了解,该WebBrowser控件是IE的包装,这不会成为问题,只是它看起来像是一个非常旧的IE版本,所有这些都涉及 CSS 错误、潜在的安全风险(如果渲染引擎没有打补丁,我真的可以期待修复无数缓冲区溢出问题吗?),以及其他问题。

I am using Visual Studio C#(express edition - does it make any difference here?)

我正在使用Visual Studio C#(快速版 - 在这里有什么不同吗?)

I would like to integrate a good web browser in my applications. In some, I just use it to handle the user registration process, interface with some of my website's features and other things of that order, but I have another application in mind that will require more err... control.

我想在我的应用程序中集成一个好的网络浏览器。在某些情况下,我只是用它来处理用户注册过程、与我网站的某些功能和其他相关内容的接口,但我有另一个应用程序需要更多的错误...控制。

I need:

我需要:

  • A browser that can integrate inside a window of my application (not a separate window)
  • A good support for CSS, js and other web technologies, on par with any modern browser
  • Basic browser functions like "navigate", "back", "reload"...
  • Liberal access to the page code and output.
  • 可以集成在我的应用程序窗口内的浏览器(不是单独的窗口)
  • 对 CSS、js 和其他 Web 技术的良好支持,可与任何现代浏览器相媲美
  • 基本的浏览器功能,如“导航”、“返回”、“重新加载”...
  • 自由访问页面代码和输出。

I was thinking about Chrome, since it comes under the BSD license, but I would be just as happy with a recent version of IE.

我在考虑 Chrome,因为它受 BSD 许可,但我对 IE 的最新版本同样满意。

As much as possible, I would like to keep things simple. The best would be if one could patch the existing WebBrowsercontrol, which does already about 70% of what I need, but I don't think that's possible.

尽可能地,我想让事情保持简单。最好的办是,如果可以修补现有WebBrowser控件,它已经完成了我需要的大约 70%,但我认为这是不可能的。

I have found an activeX control for Mozilla (http://www.iol.ie/~locka/mozilla/control.htm) but it looks like it's an old version, so it's not necessarily an improvement.

我找到了 Mozilla ( http://www.iol.ie/~locka/mozilla/control.htm)的 activeX 控件,但它看起来像是旧版本,因此不一定是改进。

I am open to suggestions

我愿意接受建议

回答by Chris S

Chrome uses (a fork of) Webkit if you didn't know, which is also used by Safari. Here's a few questions that are of the same vein:

如果您不知道,Chrome 使用(一个分支)Webkit,Safari 也使用它。这里有几个相同的问题:

The webkit one isn't great as the other answer states, one version no longer works (the google code one) and the Mono one is experimental. It'd be nice if someone made the effort to make a decent .NET wrapper for it but it's not something anyone seems to want to do - which is surprising given it now has support for HTML5 and so many other features that the IE(8) engine lacks.

webkit 版本并不像其他答案所说的那样好,一个版本不再有效(谷歌代码版本),而 Mono 版本是实验性的。如果有人努力为它制作一个像样的 .NET 包装器,那会很好,但这似乎不是任何人想要做的 - 鉴于它现在支持 HTML5 和 IE(8) 的许多其他功能,这令人惊讶) 引擎缺乏。

Update (2014)

更新 (2014)

There's new dual-licensed project that allows you embed Chrome into your .NET applications called Awesomium. It comes with a .NET api but requires quite a few hacks for rendering (the examples draw the browser window to a buffer, paint the buffer as an image and refresh on a timer).

有一个新的双重许可项目,允许您将 Chrome 嵌入到名为Awesomium的 .NET 应用程序中。它带有一个 .NET api,但需要相当多的渲染技巧(示例将浏览器窗口绘制到缓冲区,将缓冲区绘制为图像并在计时器上刷新)。

I thinkthis is the browser used by Origin in Battlefield 3.

认为这是Origin在战地3中使用的浏览器。

Update (2016)

更新 (2016)

There is now DotnetBrowser, a commercial alternative to Awesomium. It's based off Chromium.

现在有了 DotnetBrowser,这是Awesomium的商业替代品。它基于 Chromium。

回答by RichieHindle

Have look at the Mono.WebBrowsercontrol - it embeds either Gecko (as used by Firefox) or WebKit (as used by Safari) into WinForms (without using ActiveX).

看看Mono.WebBrowser控件——它将 Gecko(由 Firefox 使用)或 WebKit(由 Safari 使用)嵌入到 WinForms(不使用 ActiveX)中。

回答by RichieHindle

Check out the GeckoFXcontrol.

查看GeckoFX控件。

回答by Teekin

I have used GeckoFX and it serves me very well. It uses the Firefox engine. The only caveat I have ever run into with it, is that it emits the "DocumentLoaded" event once before the document is actually fully loaded... I think it shoots out one when the HTML is loaded, and then another one when all the images and stuff are loaded. I've fixed it by simply ignoring the first one, and it has worked beautifully without fail so far.

我使用过 GeckoFX,它非常适合我。它使用火狐引擎。我遇到的唯一警告是它在文档实际完全加载之前发出一次“DocumentLoaded”......我认为它在加载 HTML 时发出一个,然后在所有图像和东西被加载。我通过简单地忽略第一个来修复它,到目前为止它运行得很好而且没有失败。

You can find it here: https://bitbucket.org/geckofx/

你可以在这里找到它:https: //bitbucket.org/geckofx/

Like most decent libraries, it is open-source.

像大多数体面的图书馆一样,它是开源的。

回答by chillitom

Checkout CefSharp.Netbindings, a project I started a while back that thankfully got picked up by the community and turned into something wonderful.

Checkout CefSharp .Netbindings,这是我不久前开始的一个项目,幸运的是它被社区接受并变成了一些很棒的东西。

The project wraps the Chromium Embedded Frameworkand has been used in a number of major projects including Rdio's Windows client, Facebook Messenger for Windows and Github for Windows.

该项目封装了Chromium 嵌入式框架,并已在多个主要项目中使用,包括 Rdio 的 Windows 客户端、Windows 的 Facebook Messenger 和 Windows 的 Github。

It features browser controls for WPFand Winformsand has tons of features and extension points. Being based on Chromium it's blisteringly fast too.

它具有WPFWinforms 的浏览器控件,并具有大量功能和扩展点。基于 Chromium,它的速度也非常快。

Grab it from NuGet: Install-Package CefSharp.Wpfor Install-Package CefSharp.WinForms

从 NuGet 获取它:Install-Package CefSharp.WpfInstall-Package CefSharp.WinForms

Check out examples and give your thoughts/feedback/pull-requests: https://github.com/cefsharp/CefSharp

查看示例并给出您的想/反馈/拉请求:https: //github.com/cefsharp/CefSharp

BSD Licensed

BSD 许可

回答by user257033

MozNet is a .NET control, previously based on the more or less abandoned GeckoFX control, but is under full development and has way more features than GeckoFX can shake a stick at. You can find it at the Se7en Softwebsite.

MozNet 是一个 .NET 控件,以前或多或少基于被放弃的 GeckoFX 控件,但正在全面开发中,并且具有比 GeckoFX 更强大的功能。您可以在Se7en Soft网站上找到它。

回答by golavietnam

You can use registry to set IE version for webbrowser control. Go to: HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION and add "yourApplicationName.exe" with value of browser_emulation To see value of browser_emulation, refer link: http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation

您可以使用注册表来设置浏览器控制的 IE 版本。转到:HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION 并添加具有 browser_emulation 值的“yourApplicationName.exe” 要查看 browser_emulation 的值,请参阅链接:http: //msdn.microsoft.com/en-us /library/ee330730%28VS.85%29.aspx#browser_emulation

回答by Vin

I've tried a handful of Webkit variants, but in my experience nothing beats the advancements that OpenWebkitSharphas. Love it love it.. HTML5 and CSS3 scores are as close as they can get to Google Chrome. Very nice API and eventing model. If you find "not implemented" APIs most likely because they are not implemented in Webkit. All else works great.

我尝试了一些 Webkit 变体,但根据我的经验,没有什么比OpenWebkitSharp的进步更胜一筹了。爱它爱它.. HTML5 和 CSS3 分数尽可能接近谷歌浏览器。非常好的 API 和模型。如果您发现“未实现”的 API 很可能是因为它们未在 Webkit 中实现。其他一切都很好。

OpenWebKitSharp is a .net wrapper for the webkit engine based on the WebKit.NET 0.5 project. It adds more functionality to the 0.5 version and it provides more methods than that build. OpenWebKitSharp supports both the Cairo build (0.5) and the nightly builds of webkit (Nightly by default). In Version 1.5 Stable and more the nightly build is included and automatically copied after building. In earlier versions this happens with the cairo build. OpenWebKitSharp currently powers GTLite Navigator, a fast, stable and flexible web browser.

OpenWebKitSharp 是基于 WebKit.NET 0.5 项目的 webkit 引擎的 .net 包装器。它为 0.5 版本添加了更多功能,并且提供了比该版本更多的方。OpenWebKitSharp 支持 Cairo 构建 (0.5) 和 webkit 的每晚构建(默认为每晚)。在 1.5 稳定版及更高版本中,包含每晚构建并在构建后自动复制。在早期版本中,这发生在 cairo 构建中。OpenWebKitSharp 目前支持 GTLite Navigator,一个快速、稳定和灵活的网络浏览器。

回答by scotru

If you are looking for a nice IE based control--check out: http://code.google.com/p/csexwb2/

如果您正在寻找一个不错的基于 IE 的控件——请查看:http: //code.google.com/p/csexwb2/

I've used it in production products. It's pretty good and has good demos although does not seem to be being maintained as well now as it used to.

我已经在生产产品中使用了它。它非常好并且有很好的演示,尽管现在似乎不像以前那样维护得很好。

回答by formatc

I've been testing alternatives to C# Web browser component for few days now and here is my list:

几天来我一直在测试 C# Web 浏览器组件的替代品,这是我的列表:

1. Using newer IE versions 8,9:

1. 使用较新的 IE 版本 8,9:

Web Browser component is IE7 not IE8? How to change this?

Web 浏览器组件是 IE7 不是 IE8?如何改变这个?

Pros:

优点:

  • Not much work required to get it running
  • some HTML5/CSS3 support if IE9, full if IE10
  • 让它运行不需要太多工作
  • IE9 支持部分 HTML5/CSS3,IE10 支持

Cons:

缺点:

  • Target machine must have target IE version installed, IE10 is still in preview on Win7
  • 目标机器必须安装目标IE版本,IE10在Win7上仍处于预览状态

This doesn't require much work and you can get some HTML5 and CSS3 support although IE9 lacks some of best CSS3 and HTML5 features. But I'm sure you could get IE10 running same way. The problem would be that target system would have to have IE10 installed, and since is still in preview on Windows 7 I would suggest against it.

这不需要太多工作,您可以获得一些 HTML5 和 CSS3 支持,尽管 IE9 缺乏一些最好的 CSS3 和 HTML5 功能。但我相信你可以让 IE10 以同样的方式运行。问题是目标系统必须安装 IE10,而且由于在 Windows 7 上仍处于预览状态,我建议反对它。

2. OpenWebKitSharp

2. OpenWebKitSharp

OpenWebKitSharp is a .net wrapper for the webkit engine based on the WebKit.NET 0.5 project. WebKit is a layout engine used by Chrome/Safari

OpenWebKitSharp 是基于 WebKit.NET 0.5 项目的 webkit 引擎的 .net 包装器。WebKit 是 Chrome/Safari 使用的布局引擎

Pros:

优点:

  • Actively developed
  • HTML5/CSS3 support
  • 积极发展
  • HTML5/CSS3 支持

Cons:

缺点:

  • Many features not implemented
  • Doesn't support x64 (App must be built for x86)
  • 许多功能未实现
  • 不支持 x64(必须为 x86 构建应用程序)

OpenWebKit is quite nice although many features are not yet implemented, I experienced few issues using it with visual studio which throws null object reference here and then in design mode, there are some js problems. Everyone using it will almost immediately notice js alert does nothing. Events like mouseup,mousedown... etc. doesn't work, js drag and drop is buggy and so on..

OpenWebKit 非常好,虽然许多功能尚未实现,但我在 Visual Studio 中使用它时遇到了一些问题,这里抛出空对象引用,然后在设计模式下,有一些 js 问题。使用它的每个人几乎都会立即注意到 js alert 什么都不做。像 mouseup、mousedown... 等不起作用,js 拖放是错误的等等..

I also had some difficulties installing it since it requires specific version of VC redistributable installed, so after exception I looked at event log, found version of VC and installed it.

我在安装它时也遇到了一些困难,因为它需要安装特定版本的 VC 可再发行组件,所以在异常之后我查看了日志,找到了 VC 版本并安装了它。

3. GeckoFX

3. GeckoFX

Pros:

优点:

  • Works on mono
  • Actively developed
  • HTML5/CSS3 support
  • 适用于单声道
  • 积极发展
  • HTML5/CSS3 支持

Cons:

缺点:

  • D?o?e?s?n?'?t? ?s?u?p?p?o?r?t? ?x?6?4? ?(?A?p?p? ?m?u?s?t? ?b?e? ?b?u?i?l?t? ?f?o?r? ?x?8?6?)? - see comments below
  • D?o?e?s?n?'?t? ?支持??x?6?4? ?(?A?p?p??m?u?s?t??b?e??b?u?i?l?t??f?o?r??x?8?6?)? - 请参阅下面的评论

GeckoFX is a cross platform Webrowser control for embedding into WinForms Applications. This can be used with .NET on Windows and with mono on Linux. Gecko is a layout engine used by Firefox.

GeckoFX 是一个跨平台的 Webrowser 控件,用于嵌入到 WinForms 应用程序中。这可以与 Windows 上的 .NET 和 Linux 上的 mono 一起使用。Gecko 是 Firefox 使用的布局引擎。

I bumped into few information that GeckoFX is not actively developed which is not true, of course it's always one or two versions behind of Firefox but that is normal, I was really impressed by activity and the control itself. It does everything I needed, but I needed some time to get it running, here's a little tutorial to get it running:

我遇到了一些关于 GeckoFX 没有积极开发的信息,这是不正确的,当然它总是比 Firefox 落后一两个版本,但这很正常,我对活动和控件本身印象深刻。它完成了我需要的一切,但我需要一些时间来让它运行,这里有一个让它运行的小教程:

  1. Download GeckoFx-Windows-16.0-0.2, here you can check if newer is available GeckoFX
  2. Add references to two downloaded dll's
  3. Since GeckoFX is wrapper you need XulRunner, go to Version Listto see which one you need
  4. Now that we know which version of XulRunner we need, we go to Mozilla XulRunner releases, go to version folder -> runtimes -> xulrunner-(your_version).en-US.win32.zip, in our case xulrunner-16.0.en-US.win32.zip
  5. Unzip everything and copy all files to your bin\Debug (or release if your project is set to release)
  6. Go to visual studio designer of your form, go to toolbox, right click inside -> Choose items -> Browse -> Find downloaded GeckoFX winforms dll file -> OK
  7. Now you should have new control GeckoWebBrowser
  1. 下载GeckoFx-Windows-16.0-0.2,在这里您可以检查是否有更新的GeckoFX
  2. 添加对两个下载的 dll 的引用
  3. 由于 GeckoFX 是包装器,因此您需要 XulRunner,请转到版本列表以查看您需要哪一个
  4. 现在我们知道我们需要哪个版本的 XulRunner,我们转到 Mozilla XulRunner 版本,转到版本文件夹 -> 运行时 -> xulrunner-(your_version).en-US.win32.zip,在我们的例子中是xulrunner-16.0.en- US.win32.zip
  5. 解压缩所有内容并将所有文件复制到 bin\Debug (如果您的项目设置为发布,则发布)
  6. 转到表单的 Visual Studio 设计器,转到工具箱,在里面右键单击 -> 选择项目 -> 浏览 -> 查找下载的 GeckoFX winforms dll 文件 -> 确定
  7. 现在你应该有新的控件 GeckoWebBrowser

If your really must use Chrome, take a look at this product called Awesomium, it's free for non-commercial projects, but license is few thousand dollars for commercial.

如果你真的必须使用 Chrome,看看这个叫做Awesomium 的产品,它对非商业项目是免费的,但商业项目的许可证是几千美元。