将 Excel 选项卡颜色设置为 Treeview 节点颜色 - 显示错误的颜色
Get Excel tab colors to Treview node color - wrong colors displayed
我正在尝试将 excel 工作表的选项卡颜色设置为树视图 nodes.backColor,但我没有获得正确的颜色而且我不确定方式(红色是蓝色,蓝色是橙色...)。请让我知道我做错了什么。
Sub sheets_loadToTreeView()
Dim WB As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim SH As Excel.Worksheet
Dim dSHeets As New Dictionary(Of String, String), dKey, tabColor
Dim dColors As New Dictionary(Of String, Color)
Dim mainNode As TreeNode, colorNode As TreeNode, shNode As TreeNode, MyNode1() As TreeNode
Dim colorname As String
For Each SH In WB.Worksheets
tabColor = color_integerToColor(SH.Tab.Color)
dSHeets.Add(SH.Name.ToString(), SH.Tab.Color)
If Not dColors.ContainsKey(SH.Tab.Color) Then
dColors.Add(SH.Tab.Color, tabColor)
End If
Next
With Me.TreeView1
.CheckBoxes = True
.Nodes.Clear()
mainNode = .Nodes.Add("sheets", "Sheets")
For Each dKey In dColors.Keys
colorName = "------------------"
colorNode = mainNode.Nodes.Add(dKey, colorName)
colorNode.Tag = "color"
colorNode.BackColor = dColors(dKey)
colorNode.ForeColor = dColors(dKey)
Next
For Each dKey In dSHeets.Keys
MyNode1 = Me.TreeView1.Nodes.Find(dSHeets(dKey), True)
If UBound(MyNode1) >= 0 Then
shNode = MyNode1(0).Nodes.Add(dKey, dKey)
shNode.Tag = "sheet"
End If
Next
.ExpandAll()
'MyNode.FirstNode.EnsureVisible()
End With
End Sub
Public Function color_toInteger(ByVal C As Color) As Integer
Return C.ToArgb
End Function
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
Return Color.FromArgb(colorValue)
End Function
谢谢@jimi,我的变量类型设置不正确,但这不是问题所在。
问题是红色和蓝色确实颠倒了,所以我需要把颜色分解成RGB,然后颠倒蓝色和红色的位置。
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
'Return Color.FromArgb(colorValue,)
'SOURCE: https://social.technet.microsoft.com/wiki/contents/articles/17432.vb-net-how-to-convert-a-32-bit-integer-into-a-color.aspx
Dim Bytes As Byte() = BitConverter.GetBytes(colorValue)
Dim Alpha As Byte = Bytes(3)
Dim Red As Byte = Bytes(0) '2
Dim Green As Byte = Bytes(1)
Dim Blue As Byte = Bytes(2) '0
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function
我正在尝试将 excel 工作表的选项卡颜色设置为树视图 nodes.backColor,但我没有获得正确的颜色而且我不确定方式(红色是蓝色,蓝色是橙色...)。请让我知道我做错了什么。
Sub sheets_loadToTreeView()
Dim WB As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim SH As Excel.Worksheet
Dim dSHeets As New Dictionary(Of String, String), dKey, tabColor
Dim dColors As New Dictionary(Of String, Color)
Dim mainNode As TreeNode, colorNode As TreeNode, shNode As TreeNode, MyNode1() As TreeNode
Dim colorname As String
For Each SH In WB.Worksheets
tabColor = color_integerToColor(SH.Tab.Color)
dSHeets.Add(SH.Name.ToString(), SH.Tab.Color)
If Not dColors.ContainsKey(SH.Tab.Color) Then
dColors.Add(SH.Tab.Color, tabColor)
End If
Next
With Me.TreeView1
.CheckBoxes = True
.Nodes.Clear()
mainNode = .Nodes.Add("sheets", "Sheets")
For Each dKey In dColors.Keys
colorName = "------------------"
colorNode = mainNode.Nodes.Add(dKey, colorName)
colorNode.Tag = "color"
colorNode.BackColor = dColors(dKey)
colorNode.ForeColor = dColors(dKey)
Next
For Each dKey In dSHeets.Keys
MyNode1 = Me.TreeView1.Nodes.Find(dSHeets(dKey), True)
If UBound(MyNode1) >= 0 Then
shNode = MyNode1(0).Nodes.Add(dKey, dKey)
shNode.Tag = "sheet"
End If
Next
.ExpandAll()
'MyNode.FirstNode.EnsureVisible()
End With
End Sub
Public Function color_toInteger(ByVal C As Color) As Integer
Return C.ToArgb
End Function
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
Return Color.FromArgb(colorValue)
End Function
谢谢@jimi,我的变量类型设置不正确,但这不是问题所在。 问题是红色和蓝色确实颠倒了,所以我需要把颜色分解成RGB,然后颠倒蓝色和红色的位置。
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
'Return Color.FromArgb(colorValue,)
'SOURCE: https://social.technet.microsoft.com/wiki/contents/articles/17432.vb-net-how-to-convert-a-32-bit-integer-into-a-color.aspx
Dim Bytes As Byte() = BitConverter.GetBytes(colorValue)
Dim Alpha As Byte = Bytes(3)
Dim Red As Byte = Bytes(0) '2
Dim Green As Byte = Bytes(1)
Dim Blue As Byte = Bytes(2) '0
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function