Menu and Toolbar Essentials
注意
Beginning with Visual Studio 2008 SDK, use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see Visual Studio Command Table (.Vsct) Files.
Menus and toolbars provide a convenient graphical way for users to access commands in your VSPackage. Commands are functions in VSPackages that accomplish tasks, such as printing a document, refreshing a view, or creating a new file. Menus and toolbars are convenient graphical ways to present your VSPackage's commands to users. Commands are functions that accomplish tasks, such as printing a document, refreshing a view, or creating a new file. Typically, related commands are clustered together on the same menu or toolbar
Menus typically are displayed as one-word strings clustered in a row at the top of the integrated development environment (IDE) or a tool window. Menus also can be displayed as the result of a right-click event, and are referred to as shortcut menus in that context. When clicked, menus expand to display one or more commands. Commands, when clicked, can carry out tasks or launch submenus that contain additional commands. Some well-known menu names are File, Edit, View, and Window. For more information, see Common Menu Tasks.
Toolbars typically are rows of buttons and other controls, such as combo boxes, list boxes, text boxes, and splitter controls. All toolbar controls are associated with commands. When you click a toolbar button, its associated command is activated. Toolbar buttons usually have icons that suggest the underlying commands, such as a printer for a Print command. In a drop-down list control, each item in the list is associated with a different command. A splitter control is a hybrid in which one side of the control is a toolbar button and the other side is a down arrow that displays additional commands when clicked. For more information, see How to: Create Toolbars for Tool Windows and How to: Add Icons to Commands on Toolbars.
When you create a command, you also must create an event handler for it. The event handler determines when the command is visible or enabled, allows you to modify its text, and ensures that the command responds appropriately ("routes") when activated. In most instances, the IDE handles commands using the IOleCommandTarget interface. Commands in Visual Studio route in a hierarchical manner, starting with the innermost command context, based on the local selection, and proceeding to the outermost context, based on the global selection. Commands added to the main menu are immediately available for scripting. For more information, see How to: Create and Handle Commands in VSPackages (C#), Command Routing in VSPackages and Selection Context Objects.
To define new menus and toolbars, you must:
Write a command table configuration (.ctc) file that describes the menus and toolbars in your VSPackage, and the commands associated with them. For more information, see Designing Command Table Configuration Files.
Compile the .ctc file with the Command Table Compiler (ctc.exe) to produce a binary .cto resource file. For more information, see How to: Create Menu Commands for a VSPackage By Using the Binary Command Table Compiler.
Include the resulting .cto file in your VSPackage project. You must add registration for the .cto data to your VSPackage and then run devenv /setup.
注意
The /setup switch analyzes menus, commands, project templates, and item templates. Templates are compressed files, which take a long time to analyze. If you are working with menus and commands, but not templates, add the /nosetupvstemplates switch to the command line to save compilation time:
devenv /setup /nosetupvstemplates
For a more detailed overview of menus and toolbars, see Introduction to Menu and Toolbar Commands.