HOWTO: Get the text editor font and colors information in a Visual Studio add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: March 2008   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

This article explains how to get programmatically the font and color information used by Visual Studio to colorize source code, XML files, etc. The font and color information can be configured in the Tools, Options window, Environment, Fonts and Colors node, Text Editor item. The text editor uses two settings common for all the text:

  • Font Family: it can be a fixed-width font, or a variable-width font.
  • Font Size: the size in points of the font.
Then, there are colorizable items such as the plaint text, selected text, keywords, identifiers, comments, etc. that can be customized in three ways:
  • Background color
  • Foreground color
  • Bold font (based on the common font family and size)

More Information

The automation model of Visual Studio (EnvDTE) provides the EnvDTE.DTE.Properties collection (see HOWTO: Getting properties from the DTE.Properties collection of Visual Studio .NET), which receives two parameters: the category and the page. For fonts and colors information of the text editor you have to use the "FontsAndColors" category and the "TextEditor" page. This returns an EnvDTE.Properties collection of items with the keys "FontFamily", "FontSize" and "FontsAndColorsItems". The EnvDTE.Property.Value property returns the font family for the first item and the font size for the second item. For the third item you have to use the EnvDTE.Property.Object property (rather than EnvDTE.Property.Value) to return a collection which happens to be of the EnvDTE.FontsAndColorItems type, whose items are of the EnvDTE.ColorableItems type (with the properties Background, ForeGround and Bold). This collection has an item for each colorable item such as keywords, identifiers, comments, etc. One caveat is that its Background and Foreground properties return unsigned integers that are OLE colors, not .NET Colors. Fortunately you can use the System.Drawing.ColorTranslator.FromOle to perform the conversion but this function requires a signed integer, so you need to convert from UInt32 to Int32 using the System.Convert class.

The following functions show how to retrieve the font family and size of the text editor, along with color and bold information of the plain text, keywords, string and comments.
 Public Sub GetTextEditorFontAndColorsItems(ByVal dte As EnvDTE.DTE)

   Const CATEGORY_FONTS_AND_COLORS As String = "FontsAndColors"
   Const PAGE_TEXT_EDITOR As String = "TextEditor"

   Const PROPERTY_FONT_SIZE As String = "FontSize"
   Const PROPERTY_FONT_FAMILY As String = "FontFamily"
   Const PROPERTY_FONTS_AND_COLORS_ITEMS As String = "FontsAndColorsItems"

   Const DISPLAY_ITEM_PLAIN_TEXT As String = "Plain Text"
   Const DISPLAY_ITEM_KEYWORD As String = "Keyword"
   Const DISPLAY_ITEM_STRING As String = "String"
   Const DISPLAY_ITEM_COMMENT As String = "Comment"

   Dim properties As EnvDTE.Properties
   Dim fontFamily As String
   Dim fontSize As Single
   Dim fontsAndColorsItems As EnvDTE.FontsAndColorsItems

   properties = dte.Properties(CATEGORY_FONTS_AND_COLORS, PAGE_TEXT_EDITOR)

   fontFamily = properties.Item(PROPERTY_FONT_FAMILY).Value.ToString
   fontSize = CType(properties.Item(PROPERTY_FONT_SIZE).Value, Single)

   MessageBox.Show("Font: " & fontFamily & " (" & fontSize.ToString & ")")

   fontsAndColorsItems = CType(properties.Item(PROPERTY_FONTS_AND_COLORS_ITEMS).Object, EnvDTE.FontsAndColorsItems)

   ShowFontAndColorInformation(fontsAndColorsItems, DISPLAY_ITEM_PLAIN_TEXT)
   ShowFontAndColorInformation(fontsAndColorsItems, DISPLAY_ITEM_KEYWORD)
   ShowFontAndColorInformation(fontsAndColorsItems, DISPLAY_ITEM_STRING)
   ShowFontAndColorInformation(fontsAndColorsItems, DISPLAY_ITEM_COMMENT)

End Sub

Public Sub ShowFontAndColorInformation(ByVal fontsAndColorsItems As EnvDTE.FontsAndColorsItems, ByVal displayItem As String)

   Dim colorableItems As EnvDTE.ColorableItems
   Dim oleColor As Integer

   Dim backgroundColor As Color
   Dim foregroundColor As Color
   Dim boldFont As Boolean

   Dim msg As String

   colorableItems = fontsAndColorsItems.Item(displayItem)

   oleColor = System.Convert.ToInt32(colorableItems.Background)
   backgroundColor = System.Drawing.ColorTranslator.FromOle(oleColor)

   oleColor = System.Convert.ToInt32(colorableItems.Foreground)
   foreGroundColor = System.Drawing.ColorTranslator.FromOle(oleColor)

   boldFont = colorableItems.Bold

   msg = displayItem & ControlChars.CrLf
   msg &= "BackGround Color: " & backgroundColor.ToString & ControlChars.CrLf
   msg &= "ForeGround Color: " & foregroundColor.ToString & ControlChars.CrLf
   msg &= "Bold Font: " & boldFont.ToString & ControlChars.CrLf


End Sub

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this