Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Changing VS Code Region Coloring Using XML

0.00/5 (No votes)
16 May 2007 1  
This article shows how to change the color schemes of code regions in VS quickly by using XML.

Introduction

Have you ever thought about converting your VS color settings into something like CSS and porting it into another Visual Studio IDE by only clicking a button? Here's how.

Background

While I was composing programs, I wanted to change the color scheme of my IDE, yet since it takes a long time for me to recover the original one, I did not do that for its complexity. But I finally really couldn't stand my color scheme, and then I wrote this -- it enables me to switch my code region color themes quickly and does not require restarting Visual Studio.

Using the code

Here's the code, and follow the steps below to use this macro.

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
  • Step 1: Open the Macros IDE [Tools->Macros->Macros IDE].
  • Step 2: In the MyMacros project in the Solution Explorer of the Macros IDE, right click, then select Add Module. Paste the code into it and the macro is ready to use by executing it in the Macro Explorer.

Context-menu items

Actually, you can add this macro as a button on a toolbar or a context-menu item. Just check out Tools->Customize->Macros and follow the on screen instructions.

IDE usage report

During my coding process, I discovered that the Macros IDE is actually very buggy. The Smart Indentation actually cannot handle classes well, it took too much time in loading the code I have just written. On top of that, the Macros IDE cannot load Windows Forms dialogs such as OpenFileDiaog. I jumped to this trick and it lengthened the time for me to finish the work.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here