用更好的浏览器替换.NET WebBrowser控件,比如Chrome?

时间:2009-04-26 09:20:30

标签: c# .net winforms webbrowser-control

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

据我所知,WebBrowser控件是 IE 的包装器,除了它看起来像是 IE ,所有这些都需要CSS搞砸,潜在的安全风险(如果渲染引擎没有打补丁,我真的可以期待修复多个缓冲区溢出问题吗?),以及其他问题。

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

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

我需要:

  • 可以集成到我的应用程序窗口内的浏览器(不是单独的窗口)
  • 对CSS,js和其他网络技术的良好支持,与任何现代浏览器相同
  • 基本浏览器功能,如“导航”,“返回”,“重新加载”......
  • 自由访问页面代码和输出。

我在考虑使用Chrome,因为它属于BSD许可证,但我会对最新版本的IE感到满意。

尽可能地,我想保持简单。最好的情况是,如果可以修补现有的WebBrowser控制,这已经占我所需要的约70%,但我不认为这是可能的。

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

我愿意接受建议

19 个答案:

答案 0 :(得分:155)

结帐 CefSharp .Net 绑定,这个项目我开始了一段时间,幸运地得到了社区的认可并变成了一些非常棒的东西。

该项目包含 Chromium Embedded Framework ,并已用于多个主要项目,包括Rdio的Windows客户端,Facebook Messenger for Windows和Github for Windows。

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

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

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

BSD许可

答案 1 :(得分:81)

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

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

更新(2014)

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

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

更新(2016)

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

答案 2 :(得分:62)

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

<强> 1。使用较新的IE版本8,9:

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

优点:

  • 运行不需要太多工作
  • 如果IE9支持HTML5 / CSS3,如果是IE10则支持

缺点:

  • 目标机器必须安装目标IE版本,IE10仍然在Win7上预览

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

<强> 2。 OpenWebKitSharp

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

优点:

  • 积极开发
  • HTML5 / CSS3支持

缺点:

  • 许多功能尚未实施
  • 不支持x64(必须为x86构建应用程序)

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

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

第3。 GeckoFX

优点:

  • 适用于单声道
  • 积极开发
  • HTML5 / CSS3支持

缺点:

  • 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̶)̶ - 见下面的评论

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

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

  1. 下载GeckoFx-Windows-16.0-0.2,您可以在此处查看是否有更新版本GeckoFX
  2. 添加对两个已下载的dll的引用
  3. 由于GeckoFX是包装器,您需要XulRunner,转到Version List以查看您需要哪一个
  4. 现在我们知道我们需要哪个版本的XulRunner,我们去了 Mozilla XulRunner releases,转到版本文件夹 - &gt;运行时 - &gt; xulrunner-(your_version).en-US.win32.zip,在我们的案例中xulrunner-16.0.en-US.win32.zip
  5. 解压缩所有内容并将所有文件复制到bin \ Debug(或释放如果 您的项目已设置为发布)
  6. 转到表单的visual studio designer,转到工具箱,右键单击里面 - &gt;选择项目 - &gt;浏览 - &gt;查找下载的GeckoFX winforms dll文件 - &gt;行
  7. 现在你应该有新的控件GeckoWebBrowser
  8. 如果您真的必须使用Chrome,请查看名为Awesomium的此产品,它对于非商业项目是免费的,但商业许可是几千美元。

答案 3 :(得分:30)

我有同样的问题,WebBrowser正在使用旧版本的IE浏览器,通过一些谷歌搜索,我遇到了以下代码,它改变了注册表并使WebBrowser尽可能使用最新的IE版本:

 public enum BrowserEmulationVersion
    {
        Default = 0,
        Version7 = 7000,
        Version8 = 8000,
        Version8Standards = 8888,
        Version9 = 9000,
        Version9Standards = 9999,
        Version10 = 10000,
        Version10Standards = 10001,
        Version11 = 11000,
        Version11Edge = 11001
    }
    public static class WBEmulator
    {
        private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";

        public static int GetInternetExplorerMajorVersion()
        {
            int result;

            result = 0;

            try
            {
                RegistryKey key;

                key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);

                if (key != null)
                {
                    object value;

                    value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);

                    if (value != null)
                    {
                        string version;
                        int separator;

                        version = value.ToString();
                        separator = version.IndexOf('.');
                        if (separator != -1)
                        {
                            int.TryParse(version.Substring(0, separator), out result);
                        }
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }
        private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";

        public static BrowserEmulationVersion GetBrowserEmulationVersion()
        {
            BrowserEmulationVersion result;

            result = BrowserEmulationVersion.Default;

            try
            {
                RegistryKey key;

                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
                if (key != null)
                {
                    string programName;
                    object value;

                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
                    value = key.GetValue(programName, null);

                    if (value != null)
                    {
                        result = (BrowserEmulationVersion)Convert.ToInt32(value);
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }
        public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
        {
            bool result;

            result = false;

            try
            {
                RegistryKey key;

                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);

                if (key != null)
                {
                    string programName;

                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);

                    if (browserEmulationVersion != BrowserEmulationVersion.Default)
                    {
                        // if it's a valid value, update or create the value
                        key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
                    }
                    else
                    {
                        // otherwise, remove the existing value
                        key.DeleteValue(programName, false);
                    }

                    result = true;
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }

        public static bool SetBrowserEmulationVersion()
        {
            int ieVersion;
            BrowserEmulationVersion emulationCode;

            ieVersion = GetInternetExplorerMajorVersion();

            if (ieVersion >= 11)
            {
                emulationCode = BrowserEmulationVersion.Version11;
            }
            else
            {
                switch (ieVersion)
                {
                    case 10:
                        emulationCode = BrowserEmulationVersion.Version10;
                        break;
                    case 9:
                        emulationCode = BrowserEmulationVersion.Version9;
                        break;
                    case 8:
                        emulationCode = BrowserEmulationVersion.Version8;
                        break;
                    default:
                        emulationCode = BrowserEmulationVersion.Version7;
                        break;
                }
            }

            return SetBrowserEmulationVersion(emulationCode);
        }
        public static bool IsBrowserEmulationSet()
        {
            return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
        }
    } 

您只需创建一个类并将此代码放入其中,然后在程序启动时运行以下代码:

 if (!WBEmulator.IsBrowserEmulationSet())
            {
                WBEmulator.SetBrowserEmulationVersion();
            }

<强> VB.NET:

Imports Microsoft.Win32
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Security
Imports System.Text
Imports System.Threading.Tasks

Public Enum BrowserEmulationVersion
    [Default] = 0
    Version7 = 7000
    Version8 = 8000
    Version8Standards = 8888
    Version9 = 9000
    Version9Standards = 9999
    Version10 = 10000
    Version10Standards = 10001
    Version11 = 11000
    Version11Edge = 11001
End Enum


Public Class WBEmulator
    Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
    Public Shared Function GetInternetExplorerMajorVersion() As Integer

        Dim result As Integer

        result = 0

        Try
            Dim key As RegistryKey
            key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey)
            If key IsNot Nothing Then
                Dim value As Object = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing))

                Dim Version As String
                Dim separator As Integer
                Version = value.ToString()
                separator = Version.IndexOf(".")
                If separator <> -1 Then
                    Integer.TryParse(Version.Substring(0, separator), result)
                End If
            End If

        Catch ex As SecurityException
            'The user does Not have the permissions required to read from the registry key.
        Catch ex As UnauthorizedAccessException
            'The user does Not have the necessary registry rights.
        Catch

        End Try
        GetInternetExplorerMajorVersion = result
    End Function
    Private Const BrowserEmulationKey = InternetExplorerRootKey + "\Main\FeatureControl\FEATURE_BROWSER_EMULATION"

    Public Shared Function GetBrowserEmulationVersion() As BrowserEmulationVersion

        Dim result As BrowserEmulationVersion
        result = BrowserEmulationVersion.Default

        Try
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
            If key IsNot Nothing Then
                Dim programName As String
                Dim value As Object
                programName = Path.GetFileName(Environment.GetCommandLineArgs()(0))
                value = key.GetValue(programName, Nothing)
                If value IsNot Nothing Then
                    result = CType(Convert.ToInt32(value), BrowserEmulationVersion)
                End If
            End If
        Catch ex As SecurityException
            'The user does Not have the permissions required to read from the registry key.
        Catch ex As UnauthorizedAccessException
            'The user does Not have the necessary registry rights.
        Catch

        End Try

        GetBrowserEmulationVersion = result
    End Function
    Public Shared Function SetBrowserEmulationVersion(BEVersion As BrowserEmulationVersion) As Boolean

        Dim result As Boolean = False

        Try
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
            If key IsNot Nothing Then
                Dim programName As String = Path.GetFileName(Environment.GetCommandLineArgs()(0))
                If BEVersion <> BrowserEmulationVersion.Default Then
                    'if it's a valid value, update or create the value
                    key.SetValue(programName, CType(BEVersion, Integer), RegistryValueKind.DWord)
                Else
                    'otherwise, remove the existing value
                    key.DeleteValue(programName, False)
                End If
                result = True
            End If
        Catch ex As SecurityException

            ' The user does Not have the permissions required to read from the registry key.

        Catch ex As UnauthorizedAccessException

            ' The user does Not have the necessary registry rights.

        End Try

        SetBrowserEmulationVersion = result
    End Function


    Public Shared Function SetBrowserEmulationVersion() As Boolean
        Dim ieVersion As Integer
        Dim emulationCode As BrowserEmulationVersion
        ieVersion = GetInternetExplorerMajorVersion()

        If ieVersion >= 11 Then

            emulationCode = BrowserEmulationVersion.Version11
        Else

            Select Case ieVersion
                Case 10
                    emulationCode = BrowserEmulationVersion.Version10
                Case 9
                    emulationCode = BrowserEmulationVersion.Version9
                Case 8
                    emulationCode = BrowserEmulationVersion.Version8
                Case Else
                    emulationCode = BrowserEmulationVersion.Version7
            End Select
        End If

        SetBrowserEmulationVersion = SetBrowserEmulationVersion(emulationCode)
    End Function

    Public Shared Function IsBrowserEmulationSet() As Boolean
        IsBrowserEmulationSet = GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default
    End Function
End Class

您可以使用它:

If Not WBEmulator.IsBrowserEmulationSet() Then
    WBEmulator.SetBrowserEmulationVersion()
End If

答案 4 :(得分:26)

您可以使用注册表为webbrowser控件设置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

答案 5 :(得分:18)

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

答案 6 :(得分:14)

我知道这不是一个“替代品”&#39; WebBrowser控件,但我有一些糟糕的渲染问题,同时显示一个使用BootStrap 3+进行布局等的页面,然后我发现了一个帖子,建议我使用以下内容。显然它是IE特有的,并告诉它使用客户端机器上的最新变体进行渲染(所以它不会使用IE7,因为我认为是默认的)

所以就这样说:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

文档头部的某处。

如果当然不是你的文件,那么这显然不会有帮助(虽然我个人认为这是一个安全漏洞,如果你正在阅读不是由你自己通过网络浏览器创建的页面控制 - 为什么不使用webbrowser!)

祝你好运。

答案 7 :(得分:12)

查看DotNetBrowser库。它提供了基于Chromium的WPF和WinForms浏览器控件,它们很容易嵌入到.NET应用程序中。它支持所有现代Web标准,包括HTML5,CSS3和JavaScript。呈现的页面与Google Chrome中的内容完全相同。

该库继承了Chromium的多进程架构 - 每个网页都在一个单独的Chromium进程中呈现,即使在插件崩溃或网页上发生任何其他意外错误之后,应用程序仍将继续工作。

以下是DotNetBrowser提供的一些其他有用功能:可以监听加载事件,处理网络活动,配置代理,模拟用户操作,使用cookie,访问和修改DOM,监听DOM事件,调用JavaScript从.NET开始,反之亦然,在网页上使用网络摄像头和麦克风,设置基于WebRTC的通信,以及more

查看API Reference了解详情。

下面的代码片段演示了如何创建BrowserView,将其嵌入到表单中,以及如何加载URL:

for (var i = 0; i < myArray.length; i++) {
    if (myArray[i].[name].IndexOf(SearchString, i)) != 1){
    return myArray[i];
    }
}

运行上面的示例后,您将获得以下输出:

enter image description here

图书馆是商业图书馆,但它可以在开源和学术项目中免费使用。商业许可证包括针对不同团队规模的支持包。也可以购买图书馆的源代码。

除了自己的页面外,该组件在Visual Studio Marketplace中以NuGet packageVSIX package的形式提供。

答案 8 :(得分:8)

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

  

OpenWebKitSharp是基于的webkit引擎的.net包装器   WebKit.NET 0.5项目。它为0.5版本增加了更多功能   它提供了比构建更多的方法。 OpenWebKitSharp支持   Cairo build(0.5)和webkit的夜间版本(Nightly   默认情况下)。在版本1.5稳定和更多夜间构建是   包含并在构建后自动复制。在早期版本中   这发生在cairo构建中。 OpenWebKitSharp目前有效   GTLite Navigator,一种快速,稳定,灵活的网络浏览器。

答案 9 :(得分:6)

试试EO.WebBrowser。它是新的,基于最新版本的Chrome浏览器。关于它的最好的部分是它将所有内容打包在一个.NET dll中,这样不仅非常容易使用和部署,而且同样的DLL支持32位和64位,因为它是.NET dll。

答案 10 :(得分:4)

我使用过GeckoFX,它非常适合我。它使用Firefox引擎。我遇到过的唯一警告就是它在文档实际完全加载之前发出一次“DocumentLoaded”事件...我认为它在加载HTML时会发出一个,然后在所有加载时发出另一个图像和东西都加载了。我通过简单地忽略第一个来修复它,到目前为止它已经很好地工作了。

您可以在此处找到它:https://bitbucket.org/geckofx/

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

答案 11 :(得分:3)

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

答案 12 :(得分:2)

EO.BrowserControl快速简便。它有Chrome浏览器引擎,适用于任何版本的.NET

https://visualstudiogallery.msdn.microsoft.com/fcee0646-42e7-484f-b22e-f8b2ace55dd7

注意:这是一个商业许可的产品,我并没有考虑到这一点。在尝试了各种其他事情后,我最终选择了这个。

答案 13 :(得分:2)

2016年更新:

我可以建议另外3个选项:

1. BrowseEmAll.Cef

.Net组件,可用于将 Chrome引擎集成到.Net应用程序中。基于CefGlue,但对最新Chrome版本的更新速度要快一些。此外,还有一个商业支持选项,对某些人来说可能会派上用场。当然,组件本身是开源

2. BrowseEmAll.Gecko

另一个.Net组件,可用于将 Firefox引擎集成到.Net应用程序中。这基于Geckofx,但与当前版本的Geckofx不同,这将适用于Firefox的正常发布版本。要使用Geckofx,您需要自己构建Firefox。再次商业支持可用但组件本身完全开源

3. BrowseEmAll Core API

您的.Net应用程序需要所有不同的浏览器吗?您可以将BrowseEmAll核心API 集成Chrome,Firefox,Webkit和Internet Explorer 到您的应用程序中。这是商业产品,但请注意。

(完全披露:我为这家公司工作,所以把我所说的一切都拿走了)

答案 14 :(得分:1)

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

我在生产产品中使用过它。这是非常好的并且有很好的演示,虽然现在似乎没有像过去那样得到维护。

答案 15 :(得分:1)

Geckofx和Webkit.net最初都很有前途,但是随着Internet Explorer的改进,它们并没有跟上Firefox和Chrome的最新状态,Webbrowser控件也是如此,尽管默认情况下它的行为与IE7无关。你有IE版本,但可以通过进入注册表并将其更改为允许HTML5的IE9来修复。

答案 16 :(得分:1)

查看GeckoFX控件。

答案 17 :(得分:0)

我已经尝试了所有的浏览器。 不幸的是,在所有情况下真正为我工作的唯一一个是EO.Browser。 我希望我能选择一个免费的解决方案,但我最终得到了EO.Browser。 它非常昂贵,但维护得很好。

答案 18 :(得分:0)

2016年更新:

对我而言,IE唯一可靠,维护的浏览器替代方案是EO.WebBrowser。 我花了几天时间和其他人一起尝试其他人。所有这些都有一些问题在某些时候停止了我的演出。