Visual Studio 插件开发(一)

本文主要讲解一些概念

文件的讲解

  1. Guids.cs
    文件内容如下
    这些都是自动生成的,guidYG_GentorPkgString 为整个Package的Id 在这里可以看到
    PkgString
    guidYG_GentorCmdSetString 也是自动生成的,这是整个CmdSet面板的Id,总之这两个Id 在
    VSPackage.vsct文件中会用到

  2. PkgCmdIDList.cs
    这里主要是对CommandId的统一管理
    这个文件正如它所描述的一样,主要对commandId 的id 进行统一管理

  3. source.extension.vsixmanifest
    对Visual Studio 扩展的配置,主要是扩展那一方面的内容,如:文件模板,项目工程模板,程序集,Visual Studio 工具包等。

  4. VSPackage.vsct
    vsct文件用来定义command table configuration ,这是个Xml文件,通过他可以对命令表单进行配置。

  5. 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值