Visual Studio Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 51. Create a Macro

Learn how to create and run macros that
automate mundane development activities.


Creating
a macro to help you with your daily tasks is an excellent way to
automate and improve repetitive operations. There are several ways to
create a macro: either recording your actions in the IDE or creating
a macro from scratch. After you have your macro built and tested, you
can also create a toolbar button that launches your macro or create a
keyboard shortcut.


6.9.1. Recording Macros


The
quickest way to automate simple macros
is by recording your interactions with the IDE.
To activate the macro recorder, go to Tools Macros
Record TemporaryMacro. This will start the macro recorder.
From this point until you stop the recording, your interactions with
the IDE will create code in the new macro. You can pause recording to
interact with the IDE without recording those actions in your macro.
The recorder toolbar is shown in Figure 6-11.


Figure 6-11. Macro recording toolbar

After you have performed the actions you want to record, select Stop
Recording from the macro recording toolbar or from the Tools
Macros menu. Visual Studio will then create the macro
corresponding to your actions.

If you select Macro Explorer from the Tools Macros menu
and open the Recording Module, you will see a new macro called
TemporaryMacro. This is where the code for your actions during
recording is created. Because the Macro Recorder will always create
the same macro routine, you must now either copy the code to another
macro module or at least rename the macro created by the recording
process, or your macro will be lost the next time you record a macro.
To change the name of the macro, right-click on the macro in the
Macro Explorer and select Rename.

Macro recording is a powerful way to quickly create fairly simple
macros, but it also has some major limitations. First, any selections
in the Solution Explorer or Class View will be specific to the file
or class selected, so it will be linked to the filename or class
name. This can be overcome by editing the recorded macro and using
variables in place of the filenames.

Second, some actions, such as attaching to a process to debug, will
not be recorded. Most of these actions can be coded into macros, but
you will have to code these by hand.


6.9.2. Creating Macros from Scratch


If the limitations of the macro recorder
make it impractical for your situation or if you are creating a
complex macro, then you can create a macro from scratch. To create
macros from scratch or to edit existing macros, you will use the
Macro IDE (Tools Macros Macros IDE).

Before looking at the Macro IDE, it is a good idea to first create a
new Macro Project to store your macros in. A Macro Project is just
like a regular Visual Studio Project in that you can use it to store
a number modules and classes that include macros. To create a new
macro project, click on Tools Macros New Macro
Project. You will then be prompted to name your project and choose
where it will be saved.

After creating a new project, you are ready to start writing new
macros using the Macro IDE. The Macro IDE is a miniature
version of Visual Studio used exclusively for creating and editing
macros. The Macro IDE is shown in Figure 6-12.


Figure 6-12. The Macro IDE

As you can see, the Macro IDE is similar to the Visual Studio IDE,
including familiar windows like the document window and the project
explorer.

Macros are written using Visual Basic .NET and a special set of
objects to access the capabilities of the IDE, allowing you to create
files, examine projects, and edit documents. Because these are COM
objects instead of part of the native .NET Framework, you will find
that IntelliSense support is often somewhat inconsistent.

The base object for almost all macro commands is
DTE , which stands
for Development Tools Extensibility [Hack #86] . It represents your Visual
Studio IDE session and has a large number of object collections and
properties. For macros in which you want to edit the text of the
current file, the ActiveDocument property will be
the one you are primarily concerned with. For operations such as Find
and Replace, there is a Find property in the
DTE. The documentation for the
DTE object is fairly complete, but often using the
macro recorder will allow you to figure out which methods or
properties are involved in a given operation.

Macros are organized into modules for convenience, so before you start
creating a macro's functions and subs, it is best to
create a new module to put them in. To add a new module, right-click
on your project in the Project Explorer and choose Add New Item. The
Add New Item dialog is shown in Figure 6-13.


Figure 6-13. Adding a new item

From this dialog, you can name your module, and when you click Open,
the module will be added to your project and opened in the document
window. From this point, you can start writing your macro.

For a short sample macro, this code will add the specified text to
the beginning of the active document:

Public Module NewModule
Public Sub SampleMacro( )
DTE.ActiveDocument.Selection.StartOfDocument( )
DTE.ActiveDocument.Selection.NewLine( )
DTE.ActiveDocument.Selection.Text = "This is a test"
DTE.ActiveDocument.Selection.NewLine( )
End Sub
End Module

The first line of the Sub called StartOfDocument()will move the cursor to the top of the document. The
second line will create a new line. The third line of this macro will
insert the string "This is a test",
and the last line will create another new line.

After you have written your macro in the Macro IDE, you can save the
file and then close the IDE. Your macro will now appear in the Macro
Explorer and can be run from the IDE.


6.9.3. Adding a Toolbar Button


Many

macros will be useful in your daily
life, so having to locate them through the Macro Explorer each time
can be inconvenient. Thankfully, macros can be added to a toolbar.

To avoid cluttering existing toolbars, it is a good idea to create a
new toolbar. First, select Customize from the View
Toolbars menu. Click the New button and specify the name for your new
toolbar, as shown in Figure 6-14.


Figure 6-14. Enter the name for the new toolbar

After creating the toolbar, a ghost of it will appear, as shown in
the bottom right of Figure 6-15. You can now select
the Commands tab where you can add your macro to the new toolbar.


Figure 6-15. Adding a macro to a toolbar

In order to select the macro, you must first select the Macros
category in the Categories listbox. After scrolling past the sample
macros (if they are installed), the macros from your own projects
will appear. Select the macro you want to create a button for and
drag it into the desired position on the toolbar where it will appear
as a button.

After creating your toolbar, you can simply close the customization
window and dock the toolbar wherever it is convenient for you in your
IDE. For more information on customizing this toolbar, including how
to create an image for your toolbar item, please refer to
[Hack #25].


6.9.4. Creating a Keyboard Shortcut


Creating

a keyboard shortcut is an even
easier way to make your macro accessible during your normal document
editing. A macro will simply appear as another command in the list
available when assigning a keyboard shortcut as described in
[Hack #24].


6.9.5. Sharing Macros


Unlike

normal
Visual Studio Projects, Macro Projects are contained in a single file
instead of a separate file for each module. If you have a module you
wish to share among multiple users, you must first export it. Here is
the process for exporting a macro:

Open the module you wish to export.

Click File Export NewModule (Ctrl-E).

Choose where to save the file to.

When you wish to import the module on another machine, simply select
Add Existing Item and locate the file you previously
exported.


6.9.6. See Also


[Hack #52].


Ben Von Handorf


/ 172