使用 XML 更改 Visual Studio 代码区域着色
本文展示了如何使用 XML 快速更改 VS 代码区域的颜色方案。
引言
你有没有想过将 VS 的颜色设置转换为类似 CSS 的东西,并通过单击按钮将其移植到另一个 Visual Studio IDE?以下是如何操作。
背景
在编写程序时,我想更改 IDE 的颜色方案,但由于恢复原始方案需要很长时间,而且过程复杂,我没有这样做。但最终我实在无法忍受我的颜色方案了,于是我编写了这个程序——它使我能够快速切换代码区域颜色主题,并且不需要重启 Visual Studio。
使用代码
以下是代码,请按照以下步骤使用此宏。
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module Theme
Public Sub LoadTheme()
Dim path As String = _
InputBox("please enter the theme file location", "load theme file")
Dim file As New Xml.XmlDocument
file.Load(path)
For Each item As Xml.XmlNode In file.ChildNodes(0).ChildNodes
Dim i1 As ColorableItems = CType(DTE.Properties("FontsAndColors", _
"TextEditor").Item("FontsAndColorsItems").Object, _
FontsAndColorsItems).Item(item.Attributes("name").Value)
i1.Background = UInteger.Parse(item.Attributes("background").Value)
i1.Foreground = UInteger.Parse(item.Attributes("foreground").Value)
If item.Attributes("bold").Value = "True" Then
i1.Bold = True
Else
i1.Bold = False
End If
Next
End Sub
Public Sub DumpTheme()
Dim path As String = _
InputBox("please enter the export file location", "load theme file")
Dim file As New Xml.XmlDocument
file.AppendChild(file.CreateElement("VSTheme"))
For Each item As ColorableItems In CType(DTE.Properties("FontsAndColors", _
"TextEditor").Item("FontsAndColorsItems").Object, FontsAndColorsItems)
Dim i As Xml.XmlNode = file.CreateElement("item")
i.Attributes.Append(file.CreateAttribute("foreground"))
i.Attributes.Append(file.CreateAttribute("background"))
i.Attributes.Append(file.CreateAttribute("bold"))
i.Attributes.Append(file.CreateAttribute("name"))
i.Attributes(0).Value = item.Foreground
i.Attributes(1).Value = item.Background
i.Attributes(2).Value = item.Bold.ToString
i.Attributes(3).Value = item.Name
file("VSTheme").AppendChild(i)
Next
file.Save(path)
End Sub
End Module
- 步骤 1:打开宏 IDE [工具->宏->宏 IDE]。
- 步骤 2:在宏 IDE 的解决方案资源管理器中的 MyMacros 项目中,右键单击,然后选择“添加模块”。将代码粘贴到其中,然后在宏资源管理器中执行它即可使用该宏。
上下文菜单项
实际上,你可以将此宏添加为工具栏上的按钮或上下文菜单项。只需查看工具->自定义->宏并按照屏幕上的说明操作即可。
IDE 使用报告
在我的编码过程中,我发现宏 IDE 实际上存在很多错误。智能缩进实际上无法很好地处理类,加载我刚编写的代码需要花费太多时间。此外,宏 IDE 无法加载 Windows Forms 对话框,例如 OpenFileDiaog
。我转向了这种技巧,这延长了我完成工作的时间。