VS 2015-WebBrowser控件在CSS中不显示交替的表格行颜色
下面的代码将使用CSS将具有交替行颜色的表写入html文件。写入磁盘后,当使用Firefox、Chrome、IE等直接查看html文件时,可以看到交替的行颜色。但是,在VS2015中,将html文件加载到WebBrowser1控件后,交替的行颜色不可见 我查看了SO和其他地方的其他帖子,很明显WebBrowser只是包装了IE,为了克服WebBrowser的问题,有一些解决方法涉及到安装第三方开源。但是,MS针对这个问题提供一个简单的解决方案怎么样?对于MS和VS2015来说,这似乎是一个未开发的问题,因为这意味着与VS2015捆绑的当前控件与CSS存在问题。有基于MS的解决方案吗VS 2015-WebBrowser控件在CSS中不显示交替的表格行颜色,css,vb.net,colors,html-table,webbrowser-control,Css,Vb.net,Colors,Html Table,Webbrowser Control,下面的代码将使用CSS将具有交替行颜色的表写入html文件。写入磁盘后,当使用Firefox、Chrome、IE等直接查看html文件时,可以看到交替的行颜色。但是,在VS2015中,将html文件加载到WebBrowser1控件后,交替的行颜色不可见 我查看了SO和其他地方的其他帖子,很明显WebBrowser只是包装了IE,为了克服WebBrowser的问题,有一些解决方法涉及到安装第三方开源。但是,MS针对这个问题提供一个简单的解决方案怎么样?对于MS和VS2015来说,这似乎是一个未开发
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
Dim sw作为新StreamWriter(文件名)
西南书写线(“”)
西南书写线(“”)
sw.WriteLine(“table.gridtable{”)
sw.WriteLine(“字体系列:verdana、arial、sans serif;”)
sw.WriteLine(“字体大小:12px;”)
西南书写线(“颜色:#333333;”)
sw.WriteLine(“边框宽度:1px;”)
sw.WriteLine(“边框颜色:#666666;”)
sw.WriteLine(“边框折叠:折叠;”)
sw.WriteLine(“}”)
sw.WriteLine(“table.gridtable th{”)
sw.WriteLine(“边框宽度:1px;”)
sw.WriteLine(“填充:8px;”)
sw.WriteLine(“边框样式:实心;”)
sw.WriteLine(“边框颜色:#666666;”)
sw.WriteLine(“背景色:#dedede;”)
sw.WriteLine(“}”)
sw.WriteLine(“table.gridtable tr{”)
sw.WriteLine(“背景色:#ffffff;”)
sw.WriteLine(“}”)
sw.WriteLine(“table.gridtable tr:n子级(偶数){”)
sw.WriteLine(“背景色:#fdf5e6;”)
sw.WriteLine(“}”)
sw.WriteLine(“table.gridtable td{”)
sw.WriteLine(“边框宽度:1px;”)
sw.WriteLine(“填充:8px;”)
sw.WriteLine(“边框样式:实心;”)
sw.WriteLine(“边框颜色:#666666;”)
sw.WriteLine(“}”)
西南书写线(“”)
西南书写线(“”)
西南书写线(“”)
软件写入(“&”输出“&”)
软件写入(“&”输出“&”)
软件写入(“&”输出“&”)
软件写入(“&”输出“&”)
西南书写线(“”)
西南书写线(“”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
西南书写线(“”)
西南书写线(“”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
西南书写线(“”)
西南书写线(“”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
西南书写线(“”)
西南书写线(“”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
sw.WriteLine(“你好”)
西南书写线(“”)
西南书写线(“”)
西南书写线(“”)
西南处置
解决方案:为了帮助他人,我将发布于的C#解决方案转换为VB.NET
首先将以下导入添加到一个独立模块(不是例如Form1):
接下来,将以下类添加到模块中:
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
在程序启动时,将进行IE仿真,以便在Webbrowser控件中观察到完整的CSS功能。查看此页面并
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
Dim IEemulation As New InternetExplorerBrowserEmulation
If Not IEemulation.IsBrowserEmulationSet() Then
IEemulation.SetBrowserEmulationVersion()
End If