您需要添加一个命令以调用 ShowToolWindow 例程。
您的包 vsct 文件的顶部需要几个外部引用:
<!--This is the file that defines the IDs for all the commands exposed by VisualStudio. -->
<Extern href="stdidcmd.h"/>
<!--This header contains the command ids for the menus provided by the shell. -->
<Extern href="vsshlids.h"/>
这个文件应该定义一些符号:
<Symbols>
<!-- Use your package guid. -->
<GuidSymbol name="guidPackage" value="{00000000-0000-0000-0000-000000000000}" />
<!-- Use a new GUID to uniquely identify your package commands -->
<GuidSymbol name="guidCmdSet" value="{11111111-1111-1111-1111-111111111111}">
<IDSymbol name="cmdidViewMyToolWindow" value="0x0500" />
</GuidSymbol>
</Symbols>
在您的包的vsct文件的
Buttons
块中添加以下内容:
<Button guid="guidCmdSet" id="cmdidViewMyToolWindow" priority="0x0100" type="Button">
<Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS0"/>
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<Strings>
<ButtonText>View &My Tool Window</ButtonText>
</Strings>
</Button>
所有这些都应该使“查看我的工具窗口”出现在视图菜单的顶部区域。现在,当有人点击它时,您需要采取行动。
您的软件包应实现IOleCommandTarget
以处理命令的可见性和启用:
public class MyPackage : Package, IOleCommandTarget
{
#region IOleCommandTarget implementation
public int QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
if ( guidCmdGroup != new Guid("{00000000-0000-0000-0000-000000000000}"))
return (int)(Constants.OLECMDERR_E_NOTSUPPORTED);
if ( cCmds == 0 || prgCmds == null || prgCmds.Length == 0 || cCmds != prgCmds.Length )
return VSConstants.E_INVALIDARG;
OLECMDF cmdf = OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED;
for ( int i = 0; i < cCmds; i++ )
prgCmds[i].cmdf = (uint)cmdf;
return VSConstants.S_OK;
}
#endregion
}
最后,在您的包初始化例程中,您告诉 shell 在单击您的命令时要执行什么操作:
protected override void Initialize()
{
base.Initialize();
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if ( null != mcs )
{
CommandID menuCommandID = new CommandID(new Guid("{11111111-1111-1111-1111-111111111111}"), (int)0x500);
MenuCommand menuItem = new MenuCommand(ShowToolWindow, menuCommandID);
mcs.AddCommand(menuItem);
}
}
在“实际”的代码中,您可能需要在C#中定义一些与vsct中定义的符号相匹配的GUID常量,并在整个代码中使用它们。