【问题标题】:Webpage works in IE, Chrome and Firefox, but not when using the .NET WebBrowser control网页在 IE、Chrome 和 Firefox 中有效,但在使用 .NET WebBrowser 控件时无效
【发布时间】:2017-02-21 11:18:30
【问题描述】:

我在 Visual Studio 2010 中使用 WebBrowser 控件并尝试显示页面:http://lk21.org

在该网页中加载了许多脚本,如果我通过 Firefox、Chrome 和最新版本的 IE 等网络浏览器打开它,它就可以正常工作。

我的问题是,当我尝试使用 WebBrowser 组件导航到该页面时,为什么它会显示“错误请求”?

看看这个:


更新:

使用 Visual Vincent 的回答可以很好地加载页面。

但是网站上的flash视频(或者我认为它类似于flash)无法播放。请参阅下图中的比较。

奇怪的是,如果我打开 YouTube,flash 运行良好。经过一番研究,它似乎是由其他原因引起的。有什么办法解决吗?

Internet Explorer - 工作正常:

WebBrowser 控件 - 由于某种原因视频卡住无法播放:

【问题讨论】:

  • YouTube 运行良好,因为它经过了非常优化,而且似乎并不总是使用 Flash(如果我没记错的话,在我的新计算机上安装 Flash 之前,我仍然可以看到 YouTube 视频)。 -- 我将研究 WebBrowser 的 Flash 问题,并希望返回一个潜在的解决方案。
  • 我已经更新了答案及其代码。
  • 我找不到更多的东西,所以除了切换浏览器之外,您现在没有什么可以做的。你可以问一个关于 flash 问题的新问题(实际上你应该首先这样做),看看是否有人能够回答它,但我不会抱太大希望。 -- 同时我建议你接受我的回答,因为它确实解决了你最初的问题。
  • 我已经编辑了您的问题以概括标题并提高整体可读性。我这样做是因为我经常在这里参考我的答案来帮助其他人解决类似的问题,但是人们通常对标题和/或问题内容感到困惑,认为这与他们的问题无关。因此,为了(希望)最大程度地减少混淆,我将问题提得更笼统。

标签: vb.net visual-studio-2010 visual-studio-2012 webbrowser-control


【解决方案1】:

这可能与WebBrowser 控件默认使用IE 7 的文档模拟模式有关,这意味着所有页面都使用Internet Explorer 7 引擎处理。由于该版本相当旧,如今大多数网站都与它不兼容,这会影响您访问该页面时的功能。

您可以通过在HKEY_LOCAL_MACHINE hive 或HKEY_CURRENT_USER 的注册表项Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION 中为您的应用程序添加一个值来更改此行为。这样做会强制您的应用程序使用特定版本的 IE 引擎。

我写了一个课程来帮助你:

'A class for changing the WebBrowser control's document emulation.
'Written by Visual Vincent, 2017.

Imports Microsoft.Win32
Imports System.Security
Imports System.Windows.Forms

Public NotInheritable Class InternetExplorer
    Private Sub New()
    End Sub

    Public Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
    Public Const BrowserEmulationKey As String = InternetExplorerRootKey & "\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
    Public Const ActiveXObjectCachingKey As String = InternetExplorerRootKey & "\MAIN\FeatureControl\FEATURE_OBJECT_CACHING"

    Private Shared ReadOnly WebBrowserInstance As New WebBrowser 'Used to get the current IE version in a .NET-friendly manner.

    Public Enum BrowserEmulation As Integer
        IE7 = 7000
        IE8 = 8000
        IE8Standards = 8888
        IE9 = 9000
        IE9Standards = 9999
        IE10 = 10000
        IE10Standards = 10001
        IE11 = 11000
        IE11Edge = 11001
    End Enum

    Public Shared Sub SetLatestBrowserEmulation(ByVal Root As RegistryRoot)
        Dim Emulation As BrowserEmulation = BrowserEmulation.IE7
        Select Case WebBrowserInstance.Version.Major
            Case Is >= 11 : Emulation = BrowserEmulation.IE11Edge
            Case 10 : Emulation = BrowserEmulation.IE10Standards
            Case 9 : Emulation = BrowserEmulation.IE9Standards
            Case 8 : Emulation = BrowserEmulation.IE8Standards
        End Select
        InternetExplorer.SetBrowserEmulation(Root, Emulation)
    End Sub

    Public Shared Sub SetBrowserEmulation(ByVal Root As RegistryRoot, ByVal Emulation As BrowserEmulation)
        Using RootKey As RegistryKey = Root.Root
            Dim EmulationKey As RegistryKey = RootKey.OpenSubKey(BrowserEmulationKey, True)
            If EmulationKey Is Nothing Then EmulationKey = RootKey.CreateSubKey(BrowserEmulationKey, RegistryKeyPermissionCheck.ReadWriteSubTree)

            Using EmulationKey
                EmulationKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(Emulation, Integer), RegistryValueKind.DWord)
            End Using
        End Using
    End Sub

    Public Shared Sub SetActiveXObjectCaching(ByVal Root As RegistryRoot, ByVal Enabled As Boolean)
        Using RootKey As RegistryKey = Root.Root
            Dim ObjectCachingKey As RegistryKey = RootKey.OpenSubKey(ActiveXObjectCachingKey, True)
            If ObjectCachingKey Is Nothing Then ObjectCachingKey = RootKey.CreateSubKey(ActiveXObjectCachingKey, RegistryKeyPermissionCheck.ReadWriteSubTree)

            Using ObjectCachingKey
                ObjectCachingKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(If(Enabled, 1, 0), Integer), RegistryValueKind.DWord)
            End Using
        End Using
    End Sub

    Public NotInheritable Class RegistryRoot
        Private _root As RegistryKey

        Public ReadOnly Property Root As RegistryKey
            Get
                Return _root
            End Get
        End Property

        Public Shared ReadOnly Property HKEY_LOCAL_MACHINE As RegistryRoot
            Get
                Return New RegistryRoot(Registry.LocalMachine)
            End Get
        End Property

        Public Shared ReadOnly Property HKEY_CURRENT_USER As RegistryRoot
            Get
                Return New RegistryRoot(Registry.CurrentUser)
            End Get
        End Property

        Private Sub New(ByVal Root As RegistryKey)
            Me._root = Root
        End Sub
    End Class
End Class

要使用它,请将这些行中的 一个 放入应用程序的 Startup 事件中:

InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_LOCAL_MACHINE)

'HKEY_CURRENT_USER is recommended if you do not want to run your application with administrative privileges.
InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER)

注意:使用HKEY_LOCAL_MACHINE root 需要管理权限)

InternetExplorer.SetLatestBrowserEmulation() 方法将在指定的注册表根目录中为您的应用程序设置浏览器模拟为 Internet Explorer 的最新安装版本

但是使用InternetExplorer.SetBrowserEmulation() 方法,您可以手动控制它应该使用哪个IE 版本(不推荐!)

了解更多:


编辑

我似乎根本无法进入那个网站,但从我读到的there have been problems with Flash hosted in the WebBrowser control.

您可以尝试禁用 ActiveX Object Caching feature,这显然会导致 Flash 控件出现一些问题。

我更新了上面的InternetExplorer 类。复制粘贴,然后将此行添加到应用程序的启动事件中:

InternetExplorer.SetActiveXObjectCaching(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER, False)

如果它仍然不起作用,那么我担心你不走运。我还没有找到其他有用的东西。

【讨论】:

  • 感谢@VisualVincent,...但这并不意味着我必须将新的浏览器版本安装到我的计算机中,不是吗?只需编辑注册表并再次运行项目就足够了,不是吗? :D
  • @gumuruh :您不必安装任何东西,不。此代码将自动使用您计算机上当前安装的 IE 版本
  • @gumuruh :恐怕你弄错了。对于 IE 8 及更高版本,它默认启用不只是适用于 IE 8。无论如何,正如我所说,Flash 并不总是在 WebBrowser 控件中正常运行是一个已知问题。除了尝试其他网络浏览器(例如GeckoFX)之外,您无能为力。
  • 我已经检查了你在上面写的内容,并且禁用缓存是潜在的解决方法......但由于它不起作用,除了切换浏览器之外你无能为力。
  • 你是对的。我现在用GeckoFX,flash视频流等所有问题都解决了! :D
【解决方案2】:

基于@Visual Vincent 的回答,我在这里做了一个重新设计的解决方案:

1 - IEBrowserEmulationMode 枚举:

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Specifies a Internet Explorer browser emulation mode.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <remarks>
''' <see href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' </remarks>
''' ----------------------------------------------------------------------------------------------------
Public Enum IEBrowserEmulationMode As Integer

    ''' <summary>
    ''' Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
    ''' </summary>
    IE7 = 7000

    ''' <summary>
    ''' Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. 
    ''' </summary>
    IE8 = 8000

    ''' <summary>
    ''' Webpages are displayed in IE8 Standards mode, regardless of the declared !DOCTYPE directive. 
    ''' <para></para>
    ''' Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    ''' </summary>
    IE8Standards = 8888

    ''' <summary>
    ''' Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
    ''' </summary>
    IE9 = 9000

    ''' <summary>
    ''' Webpages are displayed in IE9 Standards mode, regardless of the declared !DOCTYPE directive. 
    ''' <para></para>
    ''' Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    ''' </summary>
    IE9Standards = 9999

    ''' <summary>
    ''' Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
    ''' </summary>
    IE10 = 10000

    ''' <summary>
    ''' Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
    ''' </summary>
    IE10Standards = 10001

    ''' <summary>
    ''' Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode.
    ''' </summary>
    IE11 = 11000

    ''' <summary>
    ''' Webpages are displayed in IE11 edge mode, regardless of the declared !DOCTYPE directive. 
    ''' <para></para>
    ''' Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    ''' </summary>
    IE11Edge = 11001

End Enum

2 - RegistryScope 枚举。

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Specifies a registry scope (a root key).
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Enum RegistryScope As Integer

    ''' <summary>
    ''' This refers to the HKEY_LOCAL_MACHINE (or HKLM) registry root key.
    ''' <para></para>
    ''' Configuration changes made on the subkeys of this root key will affect all users.
    ''' </summary>
    Machine = 0

    ''' <summary>
    ''' This refers to the HKEY_CURRENT_USER (or HKCU) registry root key.
    ''' <para></para>
    ''' Configuration changes made on the subkeys of this root key will affect only the current user.
    ''' </summary>
    CurrentUser = 1

End Enum

3 - BrowserEmulationMode 属性,用于获取或设置当前应用程序的 IE 浏览器仿真模式。

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets or sets the Internet Explorer browser emulation mode for the current application.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example to get, set and verify the IE browser emulation mode for the current process.
''' <code>
''' Dim scope As RegistryScope = RegistryScope.CurrentUser
''' Dim oldMode As IEBrowserEmulationMode
''' Dim newMode As IEBrowserEmulationMode
''' 
''' oldMode = BrowserEmulationMode(scope)
''' BrowserEmulationMode(scope) = IEBrowserEmulationMode.IE11Edge
''' newMode = BrowserEmulationMode(scope)
''' 
''' Console.WriteLine(String.Format("Old Mode: {0} ({1})", oldMode, CStr(oldMode)))
''' Console.WriteLine(String.Format("New Mode: {0} ({1})", newMode, CStr(newMode)))
''' 
''' Dim f As New Form() With {.Size = New Size(1280, 720)}
''' Dim wb As New WebBrowser With {.Dock = DockStyle.Fill}
''' f.Controls.Add(wb)
''' f.Show()
''' wb.Navigate("http://www.whatversion.net/browser/")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="scope">
''' The registry scope.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' The Internet Explorer browser emulation mode.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Shared Property BrowserEmulationMode(ByVal scope As RegistryScope) As IEBrowserEmulationMode
    <DebuggerStepThrough>
    Get
        Return GetIEBrowserEmulationMode(Process.GetCurrentProcess().ProcessName, scope)
    End Get
    <DebuggerStepThrough>
    Set(value As IEBrowserEmulationMode)
        SetIEBrowserEmulationMode(Process.GetCurrentProcess().ProcessName, scope, value)
    End Set
End Property

3 - GetIEBrowserEmulationMode 函数和 SetIEBrowserEmulationMode 方法,用于获取或设置外部应用程序的 IE 浏览器仿真模式。

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the Internet Explorer browser emulation mode for the specified process.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim processName As String = Process.GetCurrentProcess().ProcessName
''' Dim scope As RegistryScope = RegistryScope.CurrentUser
''' Dim mode As IEBrowserEmulationMode = GetIEBrowserEmulationMode(processName, scope)
''' 
''' Console.WriteLine(String.Format("Mode: {0} ({1})", mode, CStr(mode)))
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="processName">
''' The process name (eg. 'cmd.exe').
''' </param>
''' 
''' <param name="scope">
''' The registry scope.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The resulting <see cref="IEBrowserEmulationMode"/>.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="NotSupportedException">
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function GetIEBrowserEmulationMode(ByVal processName As String, ByVal scope As RegistryScope) As IEBrowserEmulationMode

    processName = Path.GetFileNameWithoutExtension(processName)

    Using rootKey As RegistryKey = If(scope = RegistryScope.CurrentUser,
                                      RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default),
                                      RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default)),
          subKey As RegistryKey = rootKey.CreateSubKey("Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION",
                                                       RegistryKeyPermissionCheck.ReadSubTree)

        Dim value As Integer =
            CInt(subKey.GetValue(String.Format("{0}.exe", processName), 0, RegistryValueOptions.None))

        ' If no browser emulation mode is retrieved from registry, then return default version for WebBrowser control.
        If (value = 0) Then
            Return IEBrowserEmulationMode.IE7
        End If

        If [Enum].IsDefined(GetType(IEBrowserEmulationMode), value) Then
            Return DirectCast(value, IEBrowserEmulationMode)

        Else
            Throw New NotSupportedException(String.Format("Undefined browser emulation value retrieved from registry: '{0}'", value))


        End If

    End Using

End Function

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the Internet Explorer browser emulation mode for the specified process.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim p As Process = Process.GetCurrentProcess()
''' Dim scope As RegistryScope = RegistryScope.CurrentUser
''' Dim mode As IEBrowserEmulationMode = GetIEBrowserEmulationMode(p, scope)
''' 
''' Console.WriteLine(String.Format("Mode: {0} ({1})", mode, CStr(mode)))
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="p">
''' The process.
''' </param>
''' 
''' <param name="scope">
''' The registry scope.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The resulting <see cref="IEBrowserEmulationMode"/>.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="NotSupportedException">
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function GetIEBrowserEmulationMode(ByVal p As Process, ByVal scope As RegistryScope) As IEBrowserEmulationMode

    Return GetIEBrowserEmulationMode(p.ProcessName, scope)

End Function

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sets the Internet Explorer browser emulation mode for the specified process.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim processName As String = Process.GetCurrentProcess().ProcessName
''' Dim scope As RegistryScope = RegistryScope.CurrentUser
''' Dim oldMode As IEBrowserEmulationMode
''' Dim newMode As IEBrowserEmulationMode
''' 
''' oldMode = GetIEBrowserEmulationMode(processName, scope)
''' SetIEBrowserEmulationMode(processName, scope, IEBrowserEmulationMode.IE11Edge)
''' newMode = GetIEBrowserEmulationMode(processName, scope)
''' 
''' Console.WriteLine(String.Format("Old Mode: {0} ({1})", oldMode, CStr(oldMode)))
''' Console.WriteLine(String.Format("New Mode: {0} ({1})", newMode, CStr(newMode)))
''' 
''' Dim f As New Form() With {.Size = New Size(1280, 720)}
''' Dim wb As New WebBrowser With {.Dock = DockStyle.Fill}
''' f.Controls.Add(wb)
''' f.Show()
''' wb.Navigate("http://www.whatversion.net/browser/")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="processName">
''' The process name (eg. 'cmd.exe').
''' </param>
''' 
''' <param name="scope">
''' The registry scope.
''' </param>
''' 
''' <param name="mode">
''' The Internet Explorer browser emulation mode to set.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="NotSupportedException">
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SetIEBrowserEmulationMode(ByVal processName As String, ByVal scope As RegistryScope, ByVal mode As IEBrowserEmulationMode)

    processName = Path.GetFileNameWithoutExtension(processName)

    Dim currentIEBrowserEmulationMode As IEBrowserEmulationMode = GetIEBrowserEmulationMode(processName, scope)
    If (currentIEBrowserEmulationMode = mode) Then
        Exit Sub
    End If

    Using rootKey As RegistryKey = If(scope = RegistryScope.CurrentUser,
                                      RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default),
                                      RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default)),
          subKey As RegistryKey = rootKey.CreateSubKey(
                    "Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION",
                    RegistryKeyPermissionCheck.ReadWriteSubTree)

        subKey.SetValue(String.Format("{0}.exe", processName),
                        DirectCast(mode, Integer), RegistryValueKind.DWord)

    End Using

End Sub

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sets the Internet Explorer browser emulation mode for the specified process.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso href="https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)"/>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim processName As Process = Process.GetCurrentProcess()
''' Dim scope As RegistryScope = RegistryScope.CurrentUser
''' Dim oldMode As IEBrowserEmulationMode
''' Dim newMode As IEBrowserEmulationMode
''' 
''' oldMode = GetIEBrowserEmulationMode(p, scope)
''' SetIEBrowserEmulationMode(p, scope, IEBrowserEmulationMode.IE11Edge)
''' newMode = GetIEBrowserEmulationMode(p, scope)
''' 
''' Console.WriteLine(String.Format("Old Mode: {0} ({1})", oldMode, CStr(oldMode)))
''' Console.WriteLine(String.Format("New Mode: {0} ({1})", newMode, CStr(newMode)))
''' 
''' Dim f As New Form() With {.Size = New Size(1280, 720)}
''' Dim wb As New WebBrowser With {.Dock = DockStyle.Fill}
''' f.Controls.Add(wb)
''' f.Show()
''' wb.Navigate("http://www.whatversion.net/browser/")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="p">
''' The process.
''' </param>
''' 
''' <param name="scope">
''' The registry scope.
''' </param>
''' 
''' <param name="mode">
''' The Internet Explorer browser emulation mode to set.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="NotSupportedException">
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SetIEBrowserEmulationMode(ByVal p As Process, ByVal scope As RegistryScope, ByVal mode As IEBrowserEmulationMode)

    SetIEBrowserEmulationMode(p.ProcessName, scope, mode)

End Sub

获取、设置和验证当前​​进程的IE浏览器仿真模式的使用示例:

Dim scope As RegistryScope = RegistryScope.CurrentUser
Dim oldMode As IEBrowserEmulationMode
Dim newMode As IEBrowserEmulationMode

oldMode = BrowserEmulationMode(scope)
BrowserEmulationMode(scope) = IEBrowserEmulationMode.IE11Edge
newMode = BrowserEmulationMode(scope)

Console.WriteLine(String.Format("Old Mode: {0} ({1})", oldMode, CStr(oldMode)))
Console.WriteLine(String.Format("New Mode: {0} ({1})", newMode, CStr(newMode)))

Dim f As New Form() With {.Size = New Size(1280, 720)}
Dim wb As New WebBrowser With {.Dock = DockStyle.Fill}
f.Controls.Add(wb)
f.Show()
wb.Navigate("http://www.whatversion.net/browser/")

获取、设置和验证特定进程的IE浏览器仿真模式的使用示例:

Dim processName As String = Process.GetCurrentProcess().ProcessName
Dim scope As RegistryScope = RegistryScope.CurrentUser
Dim oldMode As IEBrowserEmulationMode
Dim newMode As IEBrowserEmulationMode

oldMode = GetIEBrowserEmulationMode(processName, scope)
SetIEBrowserEmulationMode(processName, scope, IEBrowserEmulationMode.IE11Edge)
newMode = GetIEBrowserEmulationMode(processName, scope)

Console.WriteLine(String.Format("Old Mode: {0} ({1})", oldMode, CStr(oldMode)))
Console.WriteLine(String.Format("New Mode: {0} ({1})", newMode, CStr(newMode)))

Dim f As New Form() With {.Size = New Size(1280, 720)}
Dim wb As New WebBrowser With {.Dock = DockStyle.Fill}
f.Controls.Add(wb)
f.Show()
wb.Navigate("http://www.whatversion.net/browser/")

【讨论】:

  • 直到现在才注意到这一点...不错的代码,虽然除此之外我无法获取当前版本我不明白你为什么觉得有必要这样做翻拍? (好吧,RegistryRoot 类可能效率不高,我实际上不知道我为什么要创建它,但仍然;)
  • @Visual Vincent,感谢您的评论。我刚刚发现并喜欢你的想法和解决方案,然后我想将这种功能实现到我的一个库中,但我不喜欢在不了解代码真正作用的情况下复制和粘贴其他代码,所以我需要重写/从头开始重构它(并调查官方来源以将 XML 文档添加到)您的原始代码看起来更可重用/更友好地用于一般需求(添加我添加的成员,重命名方法,并删除我认为不必要的成员),最后我只是分享了我所做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多