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
 

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
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOne more humble opinionmemberMember 433593318 Jul '09 - 23:56 
Thank you for code sharing! It's usefull. Cool | :cool:
The thing you did is nice, but the code is horrible. Yes, I know that VS API for Add-ins and Macros is not very comfortable in use Dead | X| , but you could clean your sample.
GeneralCoolmemberZiad Elmalki23 Jun '09 - 20:40 
Cool Lacus Smile | :)
GeneralVisual Studio 2005 Export/Import SettingsmemberSoniqueShock21 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?membernsimeonov21 May '07 - 20:04 
Tools -> Import/Export Settings seems to be working fine for me.
 
Also you could export the registry settings as well... just wondering why you're re-inventing the wheel...

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

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