コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Visual Studio 拡張機能作成】コマンド実行後にコマンド名を変更する方法

方法

.vsct ファイルの「Button」に
<CommandFlag>TextChanges</CommandFlag> を追加する

<Buttons>
  <Button guid="guidCommand1PackageCmdSet" id="Command1Id" priority="0x0100" type="Button">
    <Parent guid="guidCommand1PackageCmdSet" id="MyMenuGroup" />
    <Icon guid="guidImages" id="bmpPic1" />
    <CommandFlag>TextChanges</CommandFlag> <!-- ★追加 -->
    <Strings>
      <ButtonText>Invoke Command1</ButtonText>
    </Strings>
  </Button>
</Buttons>

コマンドクラスのコンストラクタで
OleMenuCommand クラスのインスタンスを作成して
BeforeQueryStatus イベントにコールバックを登録する

var commandService = ServiceProvider.GetService( typeof( IMenuCommandService ) ) as OleMenuCommandService;
if ( commandService == null ) return;

var menuCommandID = new CommandID( CommandSet, CommandId );
var menuItem = new OleMenuCommand( MenuItemCallback, menuCommandID );
menuItem.BeforeQueryStatus += new EventHandler( OnBeforeQueryStatus );
commandService.AddCommand( menuItem );

そして、登録したコールバックで下記のような処理を記述する

private void OnBeforeQueryStatus( object sender, EventArgs e )
{
    var myCommand = sender as OleMenuCommand;
    if ( null == myCommand ) return;
    myCommand.Text = "New Text";
}

参考サイト様

https://msdn.microsoft.com/ja-jp/library/bb165667.aspx