Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / Visual Basic

Changing VS Code Region Coloring Using XML

Rate me:
Please Sign up or sign in to vote.
1.40/5 (3 votes)
16 May 2007CPOL1 min read 19K   15   4
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.

VB
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)


Written By
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 Pin
Member 433593318-Jul-09 23:56
Member 433593318-Jul-09 23:56 
GeneralCool Pin
Ziad Elmalki23-Jun-09 20:40
Ziad Elmalki23-Jun-09 20:40 
GeneralVisual Studio 2005 Export/Import Settings Pin
Srsly21-May-07 21:50
Srsly21-May-07 21:50 
* On the VS2005 main menu, select 'Tools>Import and Export Settings...'
* From the 'Import and Export Settings Wizard' select 'Export selected environment settings' and click OK.
* You can now select which settings you want to export. To export the Fonts and Colors, check the tree node: 'All Settings>Options>Environment>Fonts and Colors'.
* Click next and specify a name for the export file.

This file can now be imported on another VS2005 instance using the same wizard.
Questionwhat about exporting your settings? Pin
nsimeonov21-May-07 20:04
nsimeonov21-May-07 20:04 

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

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