本文主要讲解一些概念
文件的讲解
Guids.cs
这些都是自动生成的,guidYG_GentorPkgString 为整个Package的Id 在这里可以看到
guidYG_GentorCmdSetString 也是自动生成的,这是整个CmdSet面板的Id,总之这两个Id 在
VSPackage.vsct文件中会用到PkgCmdIDList.cs
这个文件正如它所描述的一样,主要对commandId 的id 进行统一管理source.extension.vsixmanifest
对Visual Studio 扩展的配置,主要是扩展那一方面的内容,如:文件模板,项目工程模板,程序集,Visual Studio 工具包等。VSPackage.vsct
vsct文件用来定义command table configuration ,这是个Xml文件,通过他可以对命令表单进行配置。vsshlids.h
文件的位置:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VSSDK\VisualStudioIntegration\Common\Inc\vsshlids.h 定义资源(按钮、菜单等标识)
开发的步骤
一、增加一个Command Id
//文件 Guids.cs
using System;
namespace YG.YG_Gentor
{
static class GuidList
{
public const string guidYG_GentorPkgString = "a6b4d6fa-bd48-4024-a314-49c2f7a71bd6";
public const string guidYG_GentorCmdSetString = "d63715b8-27c4-41a7-a2af-f86c586c1d5a";
public static readonly Guid guidYG_GentorCmdSet = new Guid(guidYG_GentorCmdSetString);
};
}
//文件PkgCmdIDList .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YG.YG_Gentor
{
static class PkgCmdIDList
{
public const uint cmdidCalculateTool = 0x101;
}
}
二、建立.vsct文件
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Extern href="stdidcmd.h"/>
<Extern href="vsshlids.h"/>
<Commands package="guidGentorPkg">
<Buttons>
<Button guid="guidGentorCmdSet" id="cmdidCalculateTool" priority="0x0100" type="Button">
<Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS1"/>
<Strings>
<CommandName>cmdidCalculateTool</CommandName>
<ButtonText>Calculate Tool Window</ButtonText>
</Strings>
</Button>
</Buttons>
</Commands>
<Symbols>
<GuidSymbol name="guidGentorPkg" value="{a6b4d6fa-bd48-4024-a314-49c2f7a71bd6}" />
<GuidSymbol name="guidGentorCmdSet" value="{d63715b8-27c4-41a7-a2af-f86c586c1d5a}">
<IDSymbol name="cmdidCalculateTool" value="0x0101"/>
</GuidSymbol>
</Symbols>
</CommandTable>
三、为Package类添加ProvideMenuResourceAttribute
//文件YG.GentorPackage.cs
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidYG_GentorPkgString)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideAutoLoad(UIContextGuids.NoSolution)]
[ProvideAutoLoad(UIContextGuids.SolutionExists)]
public sealed class YG_GentorPackage : Package
{
public const string guidVSPackagePkg = "a6b4d6fa-bd48-4024-a314-49c2f7a71bd6";
public YG_GentorPackage()
{
}
#region Package Members
protected override void Initialize()
{
base.Initialize();
#region 插件初始化动作
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (mcs!=null)
{
CommandID menuCommandId = new CommandID(GuidList.guidYG_GentorCmdSet,(int)PkgCmdIDList.cmdidCalculateTool);
MenuCommand menuItem = new MenuCommand(ShowCalculateToolCallback, menuCommandId);
mcs.AddCommand(menuItem);
}
#endregion
}
private void ShowCalculateToolCallback(object sender, EventArgs e)
{
MessageBox.Show("Calculate Tool Window is dispaly", "Tool Window");
}
#endregion
}
这里主要的是修改项目工程文件关键部分如下:
<VSCTCompile Include="VSPackage.vsct">
<ResourceName>Menus.ctmenu</ResourceName>
<SubType>Designer</SubType>
</VSCTCompile>
Menus.ctmenu 与类中的[ProvideMenuResource(“Menus.ctmenu”, 1)] 第一个参数一致
五、创建命令处理方法
//文件YG.GentorPackage.cs中
private void ShowCalculateToolCallback(object sender, EventArgs e)
{
MessageBox.Show("Calculate Tool Window is dispaly", "Tool Window");
}
六、把事件处理方法和命令关联起来
//文件YG.GentorPackage.cs中
#region 插件初始化动作
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (mcs!=null)
{
CommandID menuCommandId = new CommandID(GuidList.guidYG_GentorCmdSet,(int)PkgCmdIDList.cmdidCalculateTool);
MenuCommand menuItem = new MenuCommand(ShowCalculateToolCallback, menuCommandId);
mcs.AddCommand(menuItem);
}
#endregion
参考文档:
http://www.cnblogs.com/default/archive/2010/03/08/1681175.html
http://www.cnblogs.com/qianlifeng/archive/2011/12/07/2279452.html