Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 52
نمايش فراداده

Hack 25. Customize Menus and Toolbars

You can make Visual Studio work just the way you want. After you create your own menu and toolbar items, you can even move these settings between machines.

Visual Studio includes an impressive number of menus and toolbars. In this hack, you will learn how you can customize them.

4.3.1. Customize Toolbars

The Visual Studio toolbar can be customized in a number of ways, including ways to add new toolbars and buttons, as well as modify and rearrange current toolbars.

4.3.1.1 Modify existing toolbars

Before looking at creating new custom toolbars, let's look at modifying the existing toolbars. One of the easiest things to do is move buttons around on an existing toolbar or move buttons from one toolbar to another. This can be done by simply holding down the Alt key and dragging and dropping a button from one toolbar to another or into a different position on the current toolbar.

You can also add and remove buttons from existing toolbars using the Customize screen, which can be accessed a couple of different ways. The first way to access this screen is to go to Tools Customize. You can also click on the small down arrow to the right of every toolbar, select Add or Remove Buttons, and then select Customize. Either approach brings you to the dialog shown in Figure 4-4.

Figure 4-4. Toolbars tab in Customize dialog

Once you are in this dialog, it puts the entire IDE in a special customization mode. You can use the tabs here to customize any of the existing toolbars. Clicking on a checkbox in the Toolbars list will cause that toolbar to be displayed on the screen, which is the first step if the toolbar you want to modify is not already shown. In this example, let's add a Rebuild Solution button to the Build Toolbar:

Click the checkbox to the left of the Build toolbar; this will show the toolbar on the screen.

Click on the Commands tab in the Customize dialog. This tab is shown in Figure 4-5.

On the Commands tab, select Build from the Categories list and then drag the Rebuild Solution command from the Commands list to your toolbar.

After performing these steps, you will have a toolbar that looks like the one in Figure 4-6.

Figure 4-5. Customize dialogCommands tab

Figure 4-6. Build toolbar

Now that you have the Rebuild Solution command on the toolbar, it would be nice to replace the text with a nice, easy-to-use icon. To do this, right-click on the item (make sure the Customize dialog is still open), and you will see the menu shown in Figure 4-7.

Figure 4-7. Right-click menu for toolbar item

From this menu, you can rename your item and work with the image of the item, as well as configure what is shown on the toolbar. Suppose you want to base your button on the Build Solution button. To derive a button from that one:

Right-click on the Build Solution icon (the second from the left in Figure 4-6), and click Copy Button Image from the menu. This will copy the image for that command to the clipboard.

Right-click on your Rebuild Solution item and click Paste Button Image from the menu. This will add the icon from the normal Build Solution item to the new item.

Next, you need to change your image a little bit so you can differentiate between the two. This can be done using the Edit Button Image command in the menu. Click on the Edit Button Image command, and you will see a small image editor that you can use to modify the image. In this example, simply replace the blue arrows of the image with red arrows. This is a minor change, but should be enough to remind you that this button is for rebuilding whereas the other button is for building.

After changing the icon, you can change the display style to Default Style; this will cause just the image to be shown on the menu.

The end result is shown in Figure 4-8.

Figure 4-8. Finished Build toolbar

4.3.1.2 Adding new toolbars

Adding a new toolbar is similar in many ways to modifying an existing toolbar. Instead of starting with an existing toolbar, you need to create your own. This is done from the Customize screen found by going to Tools Customize. On this screen, you need to click the New button. You will then be prompted to name your new toolbar. After you name the toolbar, it will be saved and will show up in the list of toolbars, and you can edit it just as if it were a preexisting system toolbar.

4.3.2. Customize Menus

Customizing menus is very similar to customizing toolbars. Open the Customize screen through Tools Customize. Once open, instead of working on the Toolbars tab, you simply switch right to the Commands tab and customize the menus from there. Once the Customize dialog is open, you can customize menus much as if they were toolbars. You simply need to click on the menu to open it, and then you can drag a new command to the menu, rearrange items, or right-click on an item to modify its image or text. Figure 4-9 shows an example of adding the Select Project Template command to the File New menu.

Figure 4-9. Adding a command to a menu

4.3.3. Reset Changes

Sometimes things get botched up. Toolbars may disappear and won't reappear. Buttons from a toolbar may not show up any more. Thankfully, Visual Studio provides a method to reset all the toolbars and menus. This can be done using the following procedure:

Close all running instances of Visual Studio.

Open the Visual Studio Command Prompt [Hack #77] .

Type devenv /setup.

This will reset all the toolbar and menu customizations and hopefully fix whatever problems you are running into.

4.3.4. Hacking the Hack

Now that you have made all these different customizations to the menus and toolbars on the IDE, how can you save these settings and move them to another computer? Unfortunately, there is no power toy available to do this automatically, but thankfully, it is not too hard. All of the changes are located in one file in your user profile. All you need to do is copy this file to the same directory on your new system. The file is called CmdUI.PRF and is located in the following directory:

\Documents and Settings\<username>\Application Data\Microsoft\VisualStudio\<7.1>\1033

All you need to do is copy this file to your new system, and all of your changes should be moved.