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


    此篇仅仅是因为好奇,实现的是完全没有价值的东西,当然,通过此篇的尝试,后续可以在适当的场景,深入的研究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名称--卸载

  • 相关阅读:
    查看eclipse web项目中jsp编译后的servlet源文件【转】【JSP】
    综合实战--文件上传系统【JDBC&IO&Socket】
    002、使用webpack的各种loader处理文件
    001、node & webpack工程手动搭建
    000、GO之特别语法糖
    000、GO之深刻理解拷贝
    000、常见算法解析
    003、GO之并发
    002、GO之反射
    001、GO之指针转换
  • 原文地址:https://www.cnblogs.com/lcawen/p/6674786.html
Copyright © 2020-2023  润新知