Click here to Skip to main content
Click here to Skip to main content

Changing VS Code Region Coloring Using XML

By , 16 May 2007
Rate this:
Please Sign up or sign in to vote.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rakusu

Hong Kong Hong Kong
Rakusu is a student who has goodcommand of ASPNET, C++ and SQL. She helps manages her school's server and write programs for her school. She also help managing servers for a small computer company. Currently she is preparing for her exams and studies.

Comments and Discussions

 
GeneralOne more humble opinion PinmemberMember 433593318-Jul-09 23:56 
GeneralCool PinmemberZiad Elmalki23-Jun-09 20:40 
GeneralVisual Studio 2005 Export/Import Settings PinmemberSoniqueShock21-May-07 21:50 
Questionwhat about exporting your settings? Pinmembernsimeonov21-May-07 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 16 May 2007
Article Copyright 2007 by Rakusu
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid