Running Custom Tracepoint Macros in Visual Studio
Figure 3: The Macros IDE is very similar to the Visual Studio IDE, and the Macro language is VB.
You can define macros as subroutines or functions, and
macros can have parameters. Macros that are run by the
Tracepoint dialog need to be subroutines without parameters
because they are invoked by MSCORLIB.DLL
and
the core library is not designed to know about your input
parameters or return functions. To recap if you want to
design a Tracepoint macro it needs to be a public subroutine
with no parameters written in VB.
As a presenter it is often the case that when focusing on a bit of code in a public forum, presented on a projector it is useful to increase the size of the fonts of the code under discussion. To that end I implemented GrowTheFonts and ShrinkTheFonts. GrowTheFonts increase the font-size in Visual Studio (not the Macros IDE), and ShrinkTheFonts decreases the font size when it's called. Listing 1 contains the implementation of GrowTheFonts and ShrinkTheFonts.
Listing 1: GrowTheFonts and ShrinkTheFonts changes the font size in the Visual Studio IDE when the macros are called.
Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports System.Diagnostics Imports System.Reflection Public Module Module1 Sub GrowTheFonts() Dim textEditorFontsAndColors As Properties textEditorFontsAndColors = _ DTE.Properties("FontsAndColors", "TextEditor") textEditorFontsAndColors.Item("FontSize").Value += 12 End Sub Sub ShrinkTheFonts() Dim textEditorFontsAndColors As Properties textEditorFontsAndColors = _ DTE.Properties("FontsAndColors", "TextEditor") textEditorFontsAndColors.Item("FontSize").Value -= 12 End Sub End Module
Listing 1
In the Macros IDE an instance of EnvDTE already exists as the DTE object. In the code example the DTE object is used to request "FontsAndColors" from the "TextEditor", where TextEditor represents the code editor in Visual Studio. Once retrieved the "FontSize" is request through the Item property and the font is arbitrarily adjusted larger or smaller as the case may be.
Note: To get an idea how extensive the extensibility model is for Visual Studio check out the object model chart at http://msdn.microsoft.com/en- us/library/za2b25t3(VS.80).aspx.
After defining your macro you will see it listed in the Tracepoint dialog as one of the available macros (see Figure 4).
Figure 4: The custom macros available from the dropdown list in the Visual Studio IDE.
Don't be confused by the simplicity of the two macros in Listing 1. The Visual Studio Environment has a very large and rich object model. If you can imagine it you can probably figure out a way to do it. For an example of how rich IDE interaction can be I encourage you to download Developer Express' CodeRush tool. CodeRush interacts in very diverse and visual ways to help you generate code, making it probably one of the best examples of what is possible.
Summary
A Tracepoint is a Breakpoint that is designed to supported integrated code tracing, running custom (or canned) macros, and optionally permitting execution to continue or stop. What you want the Tracepoint to do when "hit" is up to you. If you can define the macro (or find the right existing one) then you can make tracing and debugging a much more detailed part of your development experience.Biography
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C# now available on Amazon.com and fine bookstores everywhere. Look for his upcoming book Teach Yourself the ADO.NET Entity Framework in 24 Hours. You may contact him for technology questions at pkimmel@softconcepts .com. Paul Kimmel is a Technical Evangelist for Developer Express, Inc, and you can ask him about Developer Express at paulk@devexpress.com and read his DX blog at http:// community.devexpress.com/blogs/paulk.
Page 2 of 2
This article was originally published on July 6, 2009