共用方式為


在工具視窗中新增捷徑功能表

本逐步解說會在工具視窗中放置捷徑功能表。 捷徑功能表是當使用者在按鈕、文字方塊或視窗背景上按右鍵時出現的功能表。 捷徑功能表上的命令的行為與其他功能表或工具列上的命令相同。 若要支援捷徑功能表,請在 .vsct 檔案中指定它,並顯示它以回應滑鼠的按右鍵動作。

工具視窗是由繼承自 ToolWindowPane 的自訂工具視窗類別中的 WPF 使用者控制項所組成。

本逐步解說說明如何藉由在 .vsct 檔案中宣告功能表項目,然後使用 Managed Package Framework 將其實作在定義工具視窗的類別中,將捷徑功能表建立為 Visual Studio 功能表。 此方法有助於存取 Visual Studio 命令、UI 元素和 Automation 物件模型。

或者,如果您的捷徑功能表無法存取 Visual Studio 功能,您可以在使用者控制項中使用 XAML 元素的 ContextMenu 屬性。 如需詳細資訊,請參閱 ContextMenu

建立工具視窗捷徑功能表套件

  1. 建立名為 TWShortcutMenu 的 VSIX 專案,並在其中新增名為 ShortcutMenu 的工具視窗範本。 如需建立工具視窗的詳細資訊,請參閱使用工具視窗建立延伸模組

指定捷徑功能表

本逐步解說所示的捷徑功能表可讓使用者從色彩清單中選擇用來填滿工具視窗背景的色彩。

  1. 在 ShortcutMenuPackage.vsct 中,尋找名為 guidShortcutMenuPackageCmdSet 的 GuidSymbol 元素,並宣告捷徑功能表、捷徑功能表群組和功能表選項。 GuidSymbol 元素現在看起來應該像這樣:

    <GuidSymbol name="guidShortcutMenuPackageCmdSet" value="{00000000-0000-0000-0000-0000}"> // your GUID here
        <IDSymbol name="ShortcutMenuCommandId" value="0x0100" />
        <IDSymbol name="ColorMenu" value="0x1000"/>
        <IDSymbol name="ColorGroup" value="0x1100"/>
        <IDSymbol name="cmdidRed" value="0x102"/>
        <IDSymbol name="cmdidYellow" value="0x103"/>
        <IDSymbol name="cmdidBlue" value="0x104"/>
    </GuidSymbol>
    
  2. 在 Buttons 元素之前,建立 Menu 元素,然後在其中定義捷徑功能表。

    <Menus>
      <Menu guid="guidShortcutMenuPackageCmdSet" id="ColorMenu" type="Context">
        <Strings>
          <ButtonText>Color change</ButtonText>
          <CommandName>ColorChange</CommandName>
        </Strings>
      </Menu>
    </Menus>
    

    捷徑功能表沒有父代,因為它不是功能表或工具列的一部分。

  3. 使用包含捷徑功能表項目的 Group 元素來建立 Groups 元素,並將群組與捷徑功能表建立關聯。

    <Groups>
        <Group guid="guidShortcutMenuPackageCmdSet" id="ColorGroup">
            <Parent guid="guidShortcutMenuPackageCmdSet" id="ColorMenu"/>
        </Group>
    </Groups>
    
  4. 在 Buttons 元素中,定義將出現在捷徑功能表上的個別命令。 Buttons 元素應如下所示:

    <Buttons>
        <Button guid="guidShortcutMenuPackageCmdSet" id="ShortcutMenuCommandId" priority="0x0100" type="Button">
            <Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS1"/>
            <Icon guid="guidImages" id="bmpPic1" />
            <Strings>
                <ButtonText>ShortcutMenu</ButtonText>
            </Strings>
        </Button>
    
        <Button guid="guidShortcutMenuPackageCmdSet" id="cmdidRed" priority="1" type="Button">
            <Parent guid="guidShortcutMenuPackageCmdSet" id="ColorGroup" />
            <Strings>
                <ButtonText>Red</ButtonText>
            </Strings>
        </Button>
    
        <Button guid="guidShortcutMenuPackageCmdSet" id="cmdidYellow" priority="3" type="Button">
            <Parent guid="guidShortcutMenuPackageCmdSet" id="ColorGroup" />
            <Strings>
                <ButtonText>Yellow</ButtonText>
            </Strings>
        </Button>
    
        <Button guid="guidShortcutMenuPackageCmdSet" id="cmdidBlue" priority="5" type="Button">
            <Parent guid="guidShortcutMenuPackageCmdSet" id="ColorGroup" />
            <Strings>
                <ButtonText>Blue</ButtonText>
            </Strings>
        </Button>
    </Buttons>
    
  5. 在 ShortcutMenuCommand.cs 中,新增命令集 GUID、捷徑功能表和功能表項目的定義。

    public const string guidShortcutMenuPackageCmdSet = "00000000-0000-0000-0000-00000000"; // your GUID will differ
    public const int ColorMenu = 0x1000;
    public const int cmdidRed = 0x102;
    public const int cmdidYellow = 0x103;
    public const int cmdidBlue = 0x104;
    

    這些是 ShortcutMenuPackage.vsct 檔案的 Symbols 區段中定義的相同命令識別碼。 此處未包含內容群組,因為只有在 .vsct 檔案中才有需要。

實作捷徑功能表

本節會實作捷徑功能表及其命令。

  1. 在 ShortcutMenu.cs 中,工具視窗可以取得功能表命令服務,但它包含的控制項無法取得。 下列步驟說明如何讓使用者控制項使用功能表命令服務。

  2. 在 ShortcutMenu.cs 中,新增下列 using 指示詞:

    using Microsoft.VisualStudio.Shell;
    using System.ComponentModel.Design;
    
  3. 覆寫工具視窗的 Initialize() 方法以取得功能表命令服務,並新增控制項,將功能表命令服務傳遞至建構函式:

    protected override void Initialize()
    {
        var commandService = (OleMenuCommandService)GetService(typeof(IMenuCommandService));
        Content = new ShortcutMenuControl(commandService);
    }
    
  4. 在 ShortcutMenu 工具視窗建構函式中,移除新增控制項的行。 建構函式看起來應該像這樣:

    public ShortcutMenu() : base(null)
    {
        this.Caption = "ShortcutMenu";
        this.BitmapResourceID = 301;
        this.BitmapIndex = 1;
    }
    
  5. 在 ShortcutMenuControl.xaml.cs中,新增功能表命令服務的私用欄位,並變更控制項建構函式以取得功能表命令服務。 然後使用功能表命令服務來新增操作功能表命令。 ShortcutMenuControl 建構函式應該看似以下程式碼。 命令處理常式會在稍後定義。

    public ShortcutMenuControl(OleMenuCommandService service)
    {
        this.InitializeComponent();
        commandService = service;
    
        if (null !=commandService)
        {
            // Create an alias for the command set guid.
            Guid guid = new Guid(ShortcutMenuCommand.guidShortcutMenuPackageCmdSet);
    
            // Create the command IDs.
            var red = new CommandID(guid, ShortcutMenuCommand.cmdidRed);
            var yellow = new CommandID(guid, ShortcutMenuCommand.cmdidYellow);
            var blue = new CommandID(guid, ShortcutMenuCommand.cmdidBlue);
    
            // Add a command for each command ID.
            commandService.AddCommand(new MenuCommand(ChangeColor, red));
            commandService.AddCommand(new MenuCommand(ChangeColor, yellow));
            commandService.AddCommand(new MenuCommand(ChangeColor, blue));
        }
    }
    
  6. 在 ShortcutMenuControl.xaml 中,將 MouseRightButtonDown 事件新增至最上層 UserControl 元素。 XAML 檔案現在看起來應該像這樣:

    <UserControl x:Class="TWShortcutMenu.ShortcutMenuControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            Background="{DynamicResource VsBrush.Window}"
            Foreground="{DynamicResource VsBrush.WindowText}"
            mc:Ignorable="d"
            d:DesignHeight="300" d:DesignWidth="300"
            Name="MyToolWindow"
            MouseRightButtonDown="MyToolWindow_MouseRightButtonDown">
        <Grid>
            <StackPanel Orientation="Vertical">
                <TextBlock Margin="10" HorizontalAlignment="Center">ShortcutMenu</TextBlock>
            </StackPanel>
        </Grid>
    </UserControl>
    
  7. 在 ShortcutMenuControl.xaml.cs中,新增事件處理常式的虛設常式。

    private void MyToolWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
    . . .
    }
    
  8. 將下列 using 指示詞新增至相同的檔案:

    using Microsoft.VisualStudio.Shell;
    using System.ComponentModel.Design;
    using System;
    using System.Windows.Input;
    using System.Windows.Media;
    
  9. 實作 MyToolWindowMouseRightButtonDown 事件,如下所示。

    private void MyToolWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (null != commandService)
        {
            CommandID menuID = new CommandID(
                new Guid(ShortcutMenuCommand.guidShortcutMenuPackageCmdSet),
                ShortcutMenuCommand.ColorMenu);
            Point p = this.PointToScreen(e.GetPosition(this));
            commandService.ShowContextMenu(menuID, (int)p.X, (int)p.Y);
        }
    }
    

    這會使用 ShowContextMenu 方法建立捷徑功能表的 CommandID 物件、識別滑鼠點按的位置,並在該位置開啟捷徑功能表。

  10. 實作命令處理常式。

    private void ChangeColor(object sender, EventArgs e)
    {
        var mc = sender as MenuCommand;
    
        switch (mc.CommandID.ID)
        {
            case ShortcutMenuCommand.cmdidRed:
                MyToolWindow.Background = Brushes.Red;
                break;
            case ShortcutMenuCommand.cmdidYellow:
                MyToolWindow.Background = Brushes.Yellow;
                break;
            case ShortcutMenuCommand.cmdidBlue:
                MyToolWindow.Background = Brushes.Blue;
                break;
        }
    }
    

    在本例中,只有一個方法會識別 CommandID 並據以設定背景色彩,來處理所有功能表項目的事件。 如果功能表項目包含不相關的命令,您要為每個命令建立個別的事件處理常式。

測試工具視窗功能

  1. 建置此專案並開始偵錯。 隨即出現實驗執行個體。

  2. 在實驗執行個體中,按一下 [檢視/ 其他視窗],然後按一下 [ShortcutMenu]。 如此應該會顯示您的工具視窗。

  3. 以滑鼠右鍵按一下工具視窗的主體。 應該會顯示具有色彩清單的捷徑功能表。

  4. 按一下捷徑功能表上的色彩。 工具視窗背景色彩應變更為選取的色彩。