共用方式為


Commands, Menus, and Toolbars

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Menus and toolbars are the way users access commands in your VSPackage. Commands are functions 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 commands to users. 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 Extending Menus and Commands.

  • Toolbars typically are rows of buttons and other controls, such as combo boxes, list boxes, text boxes, and menu controllers. 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 menu controller 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 Adding a Menu Controller to a Toolbar.

  • 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 MenuCommands Vs. OleMenuCommands and Selection Context Objects.

    To define new menus and toolbars, you must describe them in an Visual Studio Command Table (.vsct) file. The Visual Studio Package Template creates this file for you, along with the necessary elements to support whatever commands, toolbars, and editors you selected in the template. Alternatively, you can write your own .vsct file, using the xml schema described here: VSCT XML Schema Reference.

    For more information about working with .vsct files, see Visual Studio Command Table (.Vsct) Files.

    The topics in this section explain how commands, menus, and toolbars work in VSPackages.

In This Section

How VSPackages Add User Interface Elements
An in-depth description of the Command Table Format specification.

Visual Studio Command Table (.Vsct) Files
Describes an XML-based syntax and compiler for command tables.

Default Command, Group, and Toolbar Placement
Describes predefined commands, groups, menus, and toolbars.

IDE-Defined Commands, Menus, and Groups
Specifies the predefined menus, commands, and command groups available for use by the Visual Studio IDE.

Command Design
Explains how to design commands.

Optimizing Menu and Toolbar Commands
Gives guidelines for commands.

Making Commands Available
Explains how to make commands available to Visual Studio.

Commands and Menus That Use Interop Assemblies
Explains how to implement commands that use interop assemblies.

Command Routing in VSPackages
Explains command routing in VSPackages.