gpt4 book ai didi

css - VS 2015 - WebBrowser 控件不会显示 CSS 中的交替表行颜色

转载 作者:行者123 更新时间:2023-11-28 04:18:54 25 4
gpt4 key购买 nike

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

我检查了 SO 和其他地方的其他帖子,很明显 WebBrowser 只是包装了 IE,为了克服 WebBrowser 的问题,有一些解决方法涉及安装第 3 方开源。但是,对于这个问题,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

最佳答案

解决方案:为了帮助他人,我转换了发布在 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 功能。

关于css - VS 2015 - WebBrowser 控件不会显示 CSS 中的交替表行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com