Visual Studio Web Applications—Visual Studio Macros
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| Visual Studio Articles |
| © 2006 M. MacDonald, M. Szpuszta |
Visual Studio Macros
One of the most exciting frills of the Visual Studio development environment is its powerful macro and add-in framework (which is largely unchanged from previous versions of Visual Studio .NET). This framework, known as the Visual Studio Automation model, provides almost 200 objects that give you unprecedented control over the IDE, including the ability to access and manipulate the current project hierarchy, the collection of open windows, and the integrated debugger. One of the most convenient and flexible Automation tools is the macro facility.
The simplest macro is a keystroke recording. To create a simple keystroke macro, select Tools > RA Macros > Record Temporary Macro from the Visual Studio menu, and press the appropriate keystrokes. Once you’re finished, click the stop button on the floating macro toolbar. You can now replay the recorded macro (with the Ctrl+Shift+P shortcut key).
Note Visual Studio allows only one recorded macro, which is overwritten every time you record a new one. To make a temporary macro permanent, you’ll need to cut and paste the code into a different subroutine.
A good way to start learning about macros is to use the record facility and then look at the code it generates. Select Tool > Macros > Macro Explorer to see a window that shows a tree of macro modules and the macros they contain (see Figure 2-29). Each macro corresponds to a Visual Basic subroutine. (Unfortunately, C# is not supported.) To edit the macro you just created, right-click the TemporaryMacro subroutine in the RecordingModule, and select Edit.

Figure 2-29. The Macro Explorer
Macro code uses a special DTE (design-time environment) object model. The DTE hierarchy provides the core features that allow you to interact with every aspect of the IDE. Some of the ingredients at your fingertips include the following:
- Window objects (used to close, rearrange, or otherwise manipulate open windows)
- Document objects (used to edit text)
- Solution and project objects (used to manage the underlying files and project collection)
- Tool-window objects (used to configure the IDE’s interface)
- Debugging objects (used for tasks such as creating breakpoints and halting execution)
- Event objects (used to react to IDE events)
- Code-manipulation objects (used to analyze your project’s code constructs)
For example, the following macro automatically lists all the files in the project that have been modified but not saved. The list is shown in the Output window.
Sub ListModifiedDocuments() Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow) Dim target As Object ' If the current window is an Output window, use it. Otherwise, use a ' helper function to find and activate the window. If (DTE.ActiveWindow Is win) Then target = win.Object Else target = GetOutputWindowPane(“Modified Documents”) target.clear() End If ' Loop through all the open documents, and if unsaved changes are detected, ' write the document name to the Output window. Dim doc As Document For Each doc In DTE.Documents If Not doc.Saved Then target.OutputString(doc.Name & " " & doc.FullName & _ Microsoft.VisualBasic.Constants.vbCrLf) End If Next End Sub
Figure 2-30 shows the result of running this macro.

Figure 2-30. Detecting changed documents
This is only one of several dozen useful macros that are included in the Samples macro project, which comes with Visual Studio 2005 (and the code download for this chapter). To learn more about Visual Studio macros and add-ins, you can consult a dedicated book on the subject, like Working with Microsoft Visual Studio 2005 (Microsoft Press, 2006).
Tip Many useful Visual Studio macros are installed by default with Visual Studio 2005. Look under the Samples group in the Macro Explorer, which has macros for adding comments, switching on and off line numbers, inserting dates and times, formatting code, and debugging. You can also download more advanced add-ins from http://msdn.microsoft.com/vstudio/downloads/samples. These samples can do everything from automating the build process and integrating with Outlook to spell-checking the text in your user interface.
|

