このコードを使用して、コードウィンドウの右クリックメニューに項目を追加しています。
public void OnConnection(
object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
object[] contextGUIDS = new object[] { };
Command codeWindowCommand = null;
CommandBarControl codeWindowButton;
CommandBar codeCommandBar;
CommandBars commandBars;
try
{
codeWindowCommand = _applicationObject.Commands.Item(
_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0);
}
catch
{
}
if (codeWindowCommand == null)
{
codeWindowCommand = _applicationObject.Commands.AddNamedCommand(
_addInInstance,
CODEWINDOW_COMMAND_NAME,
CODEWINDOW_COMMAND_NAME,
"Pastebin selected code",
true,
18,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported +
(int)vsCommandStatus.vsCommandStatusEnabled);
}
commandBars = (CommandBars)_applicationObject.CommandBars;
codeCommandBar = commandBars["Code Window"];
codeWindowButton = (CommandBarControl)codeWindowCommand.AddControl(
codeCommandBar, codeCommandBar.Controls.Count + 1);
codeWindowButton.Caption = "Text for button";
codeWindowButton.TooltipText = "Tooltip for button";
}
アドインは自動起動に設定されています。ただし、VS2008を実行するたびに、アドインを完全に削除するまで、メニューに別のボタンが追加されます。誰かが私がこれを修正する方法を知っていますか?
たとえば、Command.AddControl()以降は、ボタンがまだ存在しない場合にのみ実行されるifでラップしますが、APIでこれを確認する方法が見つからないようです。