码迷,mamicode.com
首页 > 其他好文 > 详细

vs 自定义插件(扩展工具)

时间:2017-04-06 18:25:03      阅读:824      评论:0      收藏:0      [点我收藏+]

标签:研究   鼠标   llb   bsp   var   活动   文本   solution   应用   

此篇仅仅是因为好奇,实现的是完全没有价值的东西,当然,通过此篇的尝试,后续可以在适当的场景,深入的研究Visual Studio自定义插件的应用。

实现功能如下:

在鼠标选中的地方,显示一下创建人,创建时间

 

1.创建一个VSIX项目

新建项目--Visual C#--Extensibility--VSIX Project

2.创建一个Command

新建项--Visual C#--Extensibility--Custom Command

3.修改Command中的MenuItemCallback回调,代码如下:

void ShowErrorMessageBox(string message)
        {
           //类似于MessageBox.Show
            VsShellUtilities.ShowMessageBox(this.ServiceProvider, message, "错误提示",
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST
                );
        }
        private void MenuItemCallback(object sender, EventArgs e)
        {
            //主要逻辑实现地方
            var dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE;
            //dte超级博大精深,逻辑实现可以通过dte作为接入口入手
            //dte.Solution.SolutionBuild.Build();//实现编译
            //dte.Solution.SolutionBuild.Run();//实现运行
            //.....
            if (dte.ActiveDocument == null)
            {
                ShowErrorMessageBox("不能插入代码,没有打开文档!");
                return;
            }
           //获取当前活动的文档作为代码文档
            var textDocument = dte.ActiveDocument.Object("TextDocument") as TextDocument;

            if (textDocument == null || textDocument.Selection == null)
            {
                ShowErrorMessageBox("不能插入代码,当前文档不是活动文档!");
                return;
            }
            else
            {
                //实现插入的文本
                string text = string.Format(@"// Author:lcw
// CreateTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                //选中位置插入
                textDocument.Selection.Insert(text, 1);
                textDocument.Selection.SmartFormat();
            }
            //string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            //string title = "MyCommand";
            //// Show a message box to prove we were here
            //VsShellUtilities.ShowMessageBox(
            //    this.ServiceProvider,
            //    message,
            //    title,
            //    OLEMSGICON.OLEMSGICON_INFO,
            //    OLEMSGBUTTON.OLEMSGBUTTON_OK,
            //    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }    

4.修改MenuItem显示名称ButtonText,在CommandPackage.vsct文件中的Button节点下:

<Button guid="guidMyCommandPackageCmdSet" id="MyCommandId" priority="0x0100" type="Button">
        <Parent guid="guidMyCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>添加注释</ButtonText>
        </Strings>
      </Button>

5.编译--双击debug下的*.vsix文件安装

6.卸载--工具--扩展和更新--vsix名称--卸载

 

vs 自定义插件(扩展工具)

标签:研究   鼠标   llb   bsp   var   活动   文本   solution   应用   

原文地址:http://www.cnblogs.com/lcawen/p/6674786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!