Author .vsct files
This document shows how to author a .vsct file to add menu items, toolbars, and other user interface (UI) elements to the Visual Studio integrated development environment (IDE). Use these steps when you add UI elements to a Visual Studio package (VSPackage) that does not already have a .vsct file.
For new projects, we recommend that you use the Visual Studio package template because it generates a .vsct file that, depending on your selections, already has the required elements for a menu command, a tool window, or a custom editor. You can modify this .vsct file to meet the requirements of your VSPackage. For more information about how to modify a .vsct file, see the examples in Extend menus and commands.
Author the file
Author a .vsct file in these phases: Create the structure for files and resources, declare the UI elements, put the UI elements in the IDE, and add any specialized behaviors.
File structure
The basic structure of a .vsct file is a CommandTable root element that contains a Commands element and a Symbols element.
To create the file structure
Add a .vsct file to your project by following the steps in How to: Create a .vsct file.
Add the required namespaces to the
CommandTable
element, as shown in the following example:<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
In the
CommandTable
element, add aCommands
element to host all of your custom menus, toolbars, command groups, and commands. So that your custom UI elements can load, theCommands
element must have itsPackage
attribute set to the name of the package.After the
Commands
element, add aSymbols
element to define the GUIDs for the package, and the names and command IDs for your UI elements.
Include Visual Studio resources
Use the Extern element to access the files that define Visual Studio commands and the menus that are required to put your UI elements in the IDE. If you will use commands defined outside your package, use the UsedCommands element to inform Visual Studio.
To include Visual Studio resources
At the top of the
CommandTable
element, add oneExtern
element for each external file to be referenced, and set thehref
attribute to the name of the file. You can reference the following header files to access Visual Studio resources:Stdidcmd.h: Defines IDs for all commands exposed by Visual Studio.
Vsshlids.h: Contains command IDs for Visual Studio menus.
If your package calls any commands that are defined by Visual Studio or by other packages, add a
UsedCommands
element after theCommands
element. Populate this element with a UsedCommand element for each command you call that is not part of your package. Set theguid
andid
attributes of theUsedCommand
elements to the GUID and ID values of the commands to call.For more information about how to find the GUIDs and IDs of Visual Studio commands, see GUIDs and IDs of Visual Studio commands. To call commands from other packages, use the GUID and the ID of the command as defined in the .vsct file for those packages.
Declare UI elements
Declare all new UI elements in the Symbols
section of the .vsct file.
To declare UI elements
In the
Symbols
element, add three GuidSymbol elements. EachGuidSymbol
element has aname
attribute and avalue
attribute. Set thename
attribute so that it reflects the purpose of the element. Thevalue
attribute takes a GUID. (To generate a GUID, on the Tools menu, select Create GUID, and then select Registry Format.)The first
GuidSymbol
element represents your package, and typically has no children. The secondGuidSymbol
element represents the command set, and will contain all of the symbols that define your menus, groups, and commands. The thirdGuidSymbol
element represents your image store and contains symbols for all of the icons for your commands. If you have no commands that use icons, you can omit the thirdGuidSymbol
element.In the
GuidSymbol
element that represents your command set, add one or more IDSymbol elements. Each of these represent a menu, toolbar, group, or command you are adding to the UI.For each
IDSymbol
element, set thename
attribute to the name you will use to refer to the corresponding menu, group, or command, and then set thevalue
element to a hexadecimal number that will represent its command ID. No twoIDSymbol
elements that have the same parent can have the same value.If any of your UI elements require icons, add an
IDSymbol
element for each icon to theGuidSymbol
element that represents your image store.
Put UI elements in the IDE
The Menus, Groups, and Buttons elements contain the definitions for all of the menus, groups, and commands that are defined in your package. Put these menus, groups, and commands in the IDE either by using a Parent element, which is part of the UI element definition, or by using a CommandPlacement element that is defined elsewhere.
Each Menu
, Group
, and Button
element has a guid
attribute and an id
attribute. Always set the guid
attribute to match the name of the GuidSymbol
element that represents your command set, and set the id
attribute to the name of the IDSymbol
element that represents your menu, group, or command in the Symbols
section.
To define UI elements
If you are defining any new menus, submenus, shortcut menus, or toolbars, add a
Menus
element to theCommands
element. Then, for each menu to be created, add a Menu element to theMenus
element.Set the
guid
andid
attributes of theMenu
element, and then set thetype
attribute to the kind of menu you want. You may also set thepriority
attribute to establish the relative position of the menu in the parent group.Note
The
priority
attribute does not apply to toolbars and context menus.All commands in the Visual Studio IDE must be hosted by command groups, which are the direct children of menus and toolbars. If you are adding new menus or toolbars to the IDE, these must contain new command groups. You may also add command groups to existing menus and toolbars so that you can visually group your commands.
When you add new command groups, you must first create a
Groups
element, and then add to it a Group element for each command group.Set the
guid
andid
attributes of eachGroup
element, and then set thepriority
attribute to establish the relative position of the group on the parent menu. For more information, see Create reusable groups of buttons.If you are adding new commands to the IDE, add a
Buttons
element to theCommands
element. Then, for each command, add a Button element to theButtons
element.Set the
guid
andid
attributes of eachButton
element, and then set thetype
attribute to the kind of button you want. You may also set thepriority
attribute to establish the relative position of the command in the parent group.Note
Use
type="button"
for standard menu commands and buttons on toolbars.In the
Button
element, add a Strings element that contains a ButtonText element and a CommandName element. TheButtonText
element provides the text label for a menu item, or the tooltip for a toolbar button. TheCommandName
element provides the name of the command to use in the command well.If your command will have an icon, create an Icon element in the
Button
element, and set itsguid
andid
attributes to theBitmap
element for the icon.Note
Toolbar buttons must have icons.
For more information, see MenuCommands vs. OleMenuCommands.
If any of your commands require icons, add a Bitmaps element to the
Commands
element. Then, for each icon, add a Bitmap element to theBitmaps
element. This is where you specify the location of the bitmap resource. For more information, see Add icons to menu commands.You can rely on the parenting structure to correctly place most menus, groups, and commands. For very large command sets, or when a menu, group, or command must appear in multiple places, we recommend that you specify command placement.
To rely on parenting to place UI elements in the IDE
For typical parenting, create a
Parent
element in eachMenu
,Group
, andCommand
element that is defined in your package.The target of the
Parent
element is the menu or group that will contain the menu, group, or command.Set the
guid
attribute to the name of theGuidSymbol
element that defines the command set. If the target element is not part of your package, use the guid for that command set, as defined in the corresponding .vsct file.Set the
id
attribute to match theid
attribute of the target menu or group. For a listing of the menus and groups that are exposed by Visual Studio, see GUIDs and IDs of Visual Studio menus or GUIDs and IDs of Visual Studio toolbars.
If you have a large number of UI elements to place in the IDE, or if you have elements that should appear in multiple places, define their placements in the CommandPlacements element, as shown in the following steps.
To use command placement to place UI elements in the IDE
After the
Commands
element, add aCommandPlacements
element.In the
CommandPlacements
element, add aCommandPlacement
element for each menu, group, or command to place.Each
CommandPlacement
element orParent
element places one menu, group, or command in one IDE location. A UI element can only have one parent, but it can have multiple command placements. To place a UI element in multiple locations, add aCommandPlacement
element for each location.Set the
guid
andid
attributes of eachCommandPlacement
element to the hosting menu or group, just as you would for aParent
element. You can also set thepriority
attribute to establish the relative position of the UI element.You can mix placement by parenting and command placement. However, for very large command sets, we recommend that you use only command placement.
Add specialized behaviors
You can use the CommandFlag element to modify the behavior of menus and commands, for example, to change their appearance and visibility. You can also affect when a command is visible by using the VisibilityConstraints element, or add keyboard shortcuts by using the KeyBindings element. Certain kinds of menus and commands already have specialized behaviors built in.
To add specialized behaviors
To make a UI element visible only in certain UI contexts, for example, when a solution is loaded, use visibility constraints.
After the
Commands
element, add aVisibilityConstraints
element.For each UI item to constrain, add a VisibilityItem element.
For each
VisibilityItem
element, set theguid
andid
attributes to the menu, group, or command, and then set thecontext
attribute to the UI context you want, as defined in the UIContextGuids80 class.
To set the visibility or availability of a UI item in code, use one or more of the following command flags:
DefaultDisabled
DefaultInvisible
DynamicItemStart
DynamicVisibility
NoShowOnMenuController
NotInTBList
For more information, see the CommandFlag element.
To change how an element appears, or change its appearance dynamically, use one or more of the following command flags:
AlwaysCreate
CommandWellOnly
DefaultDocked
DontCache
DynamicItemStart
FixMenuController
IconAndText
Pict
StretchHorizontally
TextMenuUseButton
TextChanges
TextOnly
For more information, see the CommandFlag element.
To change how an element reacts when it receives commands, use one or more of the following command flags:
AllowParams
CaseSensitive
CommandWellOnly
FilterKeys
NoAutoComplete
NoButtonCustomize
NoKeyCustomize
NoToolbarClose
PostExec
RouteToDocs
TextIsAnchorCommand
For more information, see the CommandFlag element.
To attach a menu-dependent keyboard shortcut to a menu or an item on a menu, add an ampersand character (&) in the
ButtonText
element for the menu or menu item. The character that follows the ampersand is the active keyboard shortcut when the parent menu is open.To attach a menu-independent keyboard shortcut to a command, use the KeyBindings element. For more information, see the KeyBinding element.
To localize menu text, use the
LocCanonicalName
element. For more information, see the Strings element.Some menu and button types include specialized behaviors. The following list describes some specialized menu and button types. For other types, see the
types
attribute descriptions in the Menu, Button, and Combo elements.Combo box: A combo box is a drop-down list that can be used on a toolbar. To add combo boxes to the UI, create a Combos element in the
Commands
element. Then add to theCombos
element aCombo
element for each combo box to add.Combo
elements have the same attributes and children asButton
elements and also haveDefaultWidth
andidCommandList
attributes. TheDefaultWidth
attribute sets the width in pixels, and theidCommandList
attribute points to a command ID that is used to populate the combo box.Menu controller: A menu controller is a button that has an arrow next to it. Clicking the arrow opens a list. To add a menu controller to the UI, create a
Menu
element and set itstype
attribute toMenuController
orMenuControllerLatched
, depending on the behavior you want. To populate a menu controller, set it as the parent of aGroup
element. The menu controller will display all children of that group on its drop-down list.