다음을 통해 공유


How Command Context Affects Available Menu Items

참고

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 XML-Based Command Table Configuration (.vsct) Files.

The combination of files and mechanisms that are active at a given time in Visual Studio determine the IDE's current context. The following mechanisms affect the availability of commands in the UI at any given time: environment, VSPackage, project, editor, tool window, and file type.

Command Visibility Contexts

When a command applies to the current context, the command displays in the UI according to the following locations, otherwise it is hidden:

  • Environment — The Visual Studio IDE. Commands provided by the IDE always are visible.

  • VSPackage — Implements one more features that are added to the IDE. Much of what you see in Visual Studio is implemented in multiple VSPackages.

  • Project — A container that produces a discrete and buildable deliverable made up of some underlying data. The format of the data is irrelevent. Solutions contain projects. Project commands appear only for the currently selected project. Project commands for inactive projects are hidden. See VISIBILITY_SECTION – VISIBILITY_END for an explanation of how UI commands can be hidden or displayed automatically.

  • Editor — A software component with at least one window that allows users to manipulate project code or other data. Only one editor is active at any given time. An editor can provide its own UI, but its command set must participate in the common IDE. Commands that apply to the active editor are visible, but commands from any other editor are hidden.

    An additional element that works closely with editors is a language service. A language service is a separate context that must process commands within the context of an editor.

  • File type — A variation of a file that can appear in an editor. An editor can have several file types on which it operates, and it can customize its UI according to the type of the file it is editing. It is up to the editor to decide which commands are appropriate for the file type active in the editor at a given time.

  • Tool window — Tool windows register their commands on the View menu on the main command bar. Once a tool window package is registered with Visual Studio, it is always available from the View menu.

Command Visibility Summary

The following points summarize the key concepts related to the command context:

  • Only one set of project commands is visible in the interface at a given time, and this set is determined by the active project. The active project is defined as the currently selected project or file within the project in Solution Explorer.

  • Only one set of editor commands is visible in the UI at a given time.

  • Tool window commands are always available.

The IDE displays the union of all IDE commands, VSPackage commands, active project commands, active editor commands, and tool window commands. For more information, see Command Routing Algorithm.

Accordingly, your commands are visible only when they are appropriate for the current command context. Visibility is further controlled by entries in the VISIBILITY_SECTION – VISIBILITY_END section of the .ctc file.

All commands available in Visual Studio are visible in the list of commands (also known as the command well) located in the Customize dialog box, which is available on the Tools menu. Some command enabling and disabling is automatically handled when the IDE makes calls to the interfaces implemented in your code. One example is when the build command (as represented by the GUID:ID command pair guidVSStd97:cmdidBuildCtx) is enabled because your project implements the IVsBuildableProjectCfg interface. (GUID:ID pairs are described in Designing Command Table Configuration Files). If it appears that a command has been unexpectedly enabled or disabled and the action is not described in the Command Table Configuration (.Ctc) Files, check to see if the command is well-known in the IDE.

See Also

Concepts

Command Routing in VSPackages

Selection Context Objects

Command Table Configuration (.Ctc) Files

How VSPackages Add User Interface Elements