gpt4 book ai didi

c# - VSIX:将工具窗口添加到“查看”->“其他窗口”

转载 作者:行者123 更新时间:2023-11-30 16:20:58 24 4
gpt4 key购买 nike

我在 VS 中使用 Package 类上的这个属性公开了一个工具窗口:

   [ProvideToolWindow(typeof(MyToolWindow),
Style = VsDockStyle.Tabbed,
Orientation = ToolWindowOrientation.Right)]

这工作正常,我可以使用以下代码以编程方式打开它:

  private void ShowToolWindow()
{
//this method will show the window if it's not active or bring it to front if it's collapsed
ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
if ((null == window) || (null == window.Frame))
{
throw new NotSupportedException();
}
IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
}

然而,当用户意外关闭窗口时,无法将其恢复。我想知道其他扩展如何将它添加到工具 -> 其他 Windows,例如 NuGet。我在 NuGet 源代码中找不到任何明显的内容。

最佳答案

您需要添加一个调用 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 block 中,添加如下内容:

<Button guid="guidCmdSet" id="cmdidViewMyToolWindow" priority="0x0100" type="Button">
<!--IDG_VS_WNDO_OTRWNDWS0 is the first group in "View|Other Windows". See
C:\Program Files (x86)\Microsoft Visual Studio 2010 SDK SP1\VisualStudioIntegration\Common\Inc
for other options. -->
<Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS0"/>
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<Strings>
<ButtonText>View &amp;My Tool Window</ButtonText>
</Strings>
</Button>

所有这些都应该使“查看我的工具窗口”出现在“查看”菜单的顶部。现在您需要在有人点击它时采取行动。

您的包应该实现 IOleCommandTarget 来处理命令可见性和启用:

public class MyPackage : Package, IOleCommandTarget
{
#region IOleCommandTarget implementation

/// <summary>
/// The VS shell calls this function to know if a menu item should be visible and
/// if it should be enabled/disabled.
/// This is called only when the package is active.
/// </summary>
/// <param name="guidCmdGroup">Guid describing which set of commands the current command(s) belong to</param>
/// <param name="cCmds">Number of commands for which status are being asked</param>
/// <param name="prgCmds">Information for each command</param>
/// <param name="pCmdText">Used to dynamically change the command text</param>
/// <returns>HRESULT</returns>
public int QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
// Filter out commands that are not defined by this package
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;

// Show and enable all commands.
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();

// Add our command handlers (commands must exist in the .vsct file)
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if ( null != mcs )
{
// "View My Tool Window" command callback
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 常量,并在整个过程中使用它们。

关于c# - VSIX:将工具窗口添加到“查看”->“其他窗口”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13665970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com