【问题标题】:VS 2015 - WebBrowser control does not reveal alternating table row colors in CSSVS 2015 - WebBrowser 控件不会在 CSS 中显示交替的表格行颜色
【发布时间】:2017-02-18 19:52:27
【问题描述】:

以下代码将使用 CSS 将具有交替行颜色的表格写入 html 文件。写入磁盘后,直接查看 html 文件时,例如Firefox、Chrome、IE 等,交替的行颜色可见。但是,在 VS2015 中,将 html 文件加载到 WebBrowser1 控件后,交替的行颜色不可见。

我检查了 SO 和其他地方的其他帖子,很明显 WebBrowser 只是包装了 IE,为了克服 WebBrowser 的问题,有一些解决方法涉及安装 3rd 方开源。但是,MS 为这个问题提供的直接解决方案怎么样?对于 MS 和 VS2015,这似乎是一个未开发的问题,因为这意味着与 VS2015 捆绑的当前控件存在 CSS 问题。有任何基于 MS 的解决方案吗?

Dim sw As New StreamWriter(filename)
sw.WriteLine("<!DOCTYPE html><html><body>")
sw.WriteLine("<style type=" & """" & "text/css" & """" & ">")
sw.WriteLine("table.gridtable {")
sw.WriteLine("  font-family: verdana,arial,sans-serif;")
sw.WriteLine("  font-size:12px;")
sw.WriteLine("  color: #333333;")
sw.WriteLine("  border-width: 1px;")
sw.WriteLine("  border-color: #666666;")
sw.WriteLine("  border-collapse: collapse;")
sw.WriteLine("}")
sw.WriteLine("table.gridtable th {")
sw.WriteLine("  border-width: 1px;")
sw.WriteLine("  padding: 8px;")
sw.WriteLine("  border-style: solid;")
sw.WriteLine("  border-color: #666666;")
sw.WriteLine("  background-color: #dedede;")
sw.WriteLine("}")
sw.WriteLine("table.gridtable tr {")
sw.WriteLine("background-color: #ffffff;")
sw.WriteLine("}")
sw.WriteLine("table.gridtable tr:nth-child(even) {")
sw.WriteLine("background-color: #fdf5e6;")
sw.WriteLine("}")
sw.WriteLine("table.gridtable td {")
sw.WriteLine("  border-width: 1px;")
sw.WriteLine("  padding: 8px;")
sw.WriteLine("  border-style: solid;")
sw.WriteLine("  border-color: #666666;")
sw.WriteLine("}")
sw.WriteLine("</style>")

sw.WriteLine("<table class=" & """" & "gridtable" & """" & " Align=center>")
sw.WriteLine("<tr>")
sw.Write("<th>" & "Output" & "</th>")
sw.Write("<th>" & "Output" & "</th>")
sw.Write("<th>" & "Output" & "</th>")
sw.Write("<th>" & "Output" & "</th>")
sw.WriteLine("</tr>")
sw.WriteLine("<tr>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("</tr>")
sw.WriteLine("<tr>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("</tr>")
sw.WriteLine("<tr>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("</tr>")
sw.WriteLine("<tr>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>")
sw.WriteLine("</tr>")
sw.WriteLine("</table>")
sw.WriteLine("</body></html>")
sw.dispose

【问题讨论】:

标签: css vb.net colors html-table webbrowser-control


【解决方案1】:

解决方案:为了帮助其他人,我将在Configuring the emulation mode of an Internet Explorer WebBrowser control, by Richard James Moss 发布的 C# 解决方案转换为 VB.NET。

首先将以下导入添加到独立模块(例如 Form1):

Imports Microsoft.Win32
Imports System.Security

接下来,将以下类添加到模块中:

Public Class InternetExplorerBrowserEmulation
    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
    Private Const BrowserEmulationKey As String = InternetExplorerRootKey & "\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
    Public Function GetBrowserEmulationVersion() As BrowserEmulationVersion
        Dim result As BrowserEmulationVersion

        result = BrowserEmulationVersion.Default

        Try
            Dim key As RegistryKey

            key = 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 e1 As SecurityException
            ' The user does not have the permissions required to read from the registry key.
        Catch e2 As UnauthorizedAccessException
            ' The user does not have the necessary registry rights.
        End Try

        Return result
    End Function

    Public Function IsBrowserEmulationSet() As Boolean
        Return GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default
    End Function

    Public Function SetBrowserEmulationVersion(ByVal browserEmulationVersion As BrowserEmulationVersion) As Boolean
        Dim result As Boolean

        result = False

        Try
            Dim key As RegistryKey

            key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)

            If key IsNot Nothing Then
                Dim programName As String

                programName = Path.GetFileName(Environment.GetCommandLineArgs()(0))

                If browserEmulationVersion <> BrowserEmulationVersion.Default Then
                    ' if it's a valid value, update or create the value
                    key.SetValue(programName, CInt(Fix(browserEmulationVersion)), RegistryValueKind.DWord)
                Else
                    ' otherwise, remove the existing value
                    key.DeleteValue(programName, False)
                End If

                result = True
            End If
        Catch e1 As SecurityException
            ' The user does not have the permissions required to read from the registry key.
        Catch e2 As UnauthorizedAccessException
            ' The user does not have the necessary registry rights.
        End Try

        Return result
    End Function

    Public 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

        Return SetBrowserEmulationVersion(emulationCode)
    End Function

    Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"

    Public Function GetInternetExplorerMajorVersion() As Integer
        Dim result As Integer = Nothing

        result = 0

        Try
            Dim key As RegistryKey

            key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey)

            If key IsNot Nothing Then
                Dim value As Object

                value = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing))

                If value IsNot Nothing Then
                    Dim version As String
                    Dim separator As Integer

                    version = value.ToString()
                    separator = version.IndexOf("."c)
                    If separator <> -1 Then
                        Integer.TryParse(version.Substring(0, separator), result)
                    End If
                End If
            End If
        Catch e1 As SecurityException
            ' The user does not have the permissions required to read from the registry key.
        Catch e2 As UnauthorizedAccessException
            ' The user does not have the necessary registry rights.
        End Try

        Return result
    End Function
End Class

最后,在 Form1_Load 过程中,添加以下代码:

Dim IEemulation As New InternetExplorerBrowserEmulation
If Not IEemulation.IsBrowserEmulationSet() Then
    IEemulation.SetBrowserEmulationVersion()
End If

在程序启动时,将进行 IE 仿真,以便在 Webbrowser 控件中观察到完整的 CSS 功能。

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多