VS程序员注释宏使用

Visual Studio添加一个工具条,工具条中实现三个功能(有三个按钮):1AddCom2ModCom3DelCom。来说明本文:给Visual Studio添加工具条


1  打开Visual Studio的宏管理器:工具--Macros资源管理器,点击【宏】,右键新建宏项目(名字为Samples),完成后,右键点击【Samples-新建模块,名字为:VSEditor

拷贝下面的代码:注意(Public ModuleComment的意思是:刚才新建的模块的名字是:VSEditor;然后添加了三个函数;SMSCAddCommentSMSCDeleteCommentSMSCModifyComment分别代表;插入分发现和插入文件注释),然后保存。


'' 版权所有 (c) Microsoft Corporation。保留所有权利。
''
'' 这些示例演示如何使用 VS 自动化对象模型。
'' 虽然这些示例不受支持,但您可以按原样使用。这些示例不是用来
'' 演示如何实现要求十分严格的命令。例如,
'' 如果某个命令要求选定文本,而您却在空行中
'' 调用该宏,则该宏的行为未定义。
''
'Option Strict Off
'Option Explicit Off


Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.ControlChars


Public Module VSEditor


    ' 一个常量字符串,某些宏使用该字符串来确定
    '  文本缓冲区内的搜索何时已经换行。应对该字符串进行本地化,
    '  以供每种语言的 Visual Studio 使用。
    Const AuthorName = "liuyaxin(00424931)"
    Const RequirementDes = "DTS2018030800792"


    '' CommentRegion 将面向行的注释语法放在
    '' 所选内容涉及的每一行的行首,并创建撤消操作
    '' 以便对编辑的所有行仅执行一次撤消操作。
    ''
    Sub SMSCAddComment()
        Dim selection As EnvDTE.TextSelection
        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As TextPoint
        Dim addBeginComment, addEndComment, description As String
        addBeginComment = "//Add BEGIN by " + AuthorName + " <" + RequirementDes + "> " + System.DateTime.Now.ToString
        description = "//Description:"
        addEndComment = "//Add END by " + AuthorName + " " + System.DateTime.Now.ToString
        selection = DTE.ActiveDocument.Selection()
        selection.Insert(addBeginComment)
        selection.NewLine()
        selection.Insert(description)
        selection.NewLine()
        selection.Insert(addEndComment)


    End Sub


    Sub SMSCModifyComment()
        Dim selection As EnvDTE.TextSelection
        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As TextPoint
        Dim addBeginComment, addEndComment, description, newComment As String
        addBeginComment = "//Modfiy BEGIN by " + AuthorName + " <" + RequirementDes + "> " + System.DateTime.Now.ToString
        description = "// Description:"
        newComment = "//--------the new is-------------------"
        addEndComment = "//Modfiy END by " + AuthorName + " " + System.DateTime.Now.ToString
        selection = DTE.ActiveDocument.Selection()
        selection.Insert(addBeginComment)
        selection.NewLine()
        selection.Insert(description)
        selection.NewLine()
        selection.Insert(newComment)
        selection.NewLine()
        selection.Insert(addEndComment)




    End Sub


    Sub SMSCDeleteComment()
        Dim selection As EnvDTE.TextSelection
        Dim addBeginComment, addEndComment, description, featureInfo As String


        selection = DTE.ActiveDocument.Selection()


        addBeginComment = "//Delete BEGIN by " + AuthorName + " <" + RequirementDes + "> " + System.DateTime.Now.ToString
        description = "// Description:"
        addEndComment = "//Delete END by " + AuthorName + " " + System.DateTime.Now.ToString


        Dim SelLineCount
        Dim i
        SelLineCount = selection.BottomLine - selection.TopLine


        selection.GotoLine(selection.TopLine)
        selection.LineUp()
        selection.EndOfLine()
        selection.NewLine()
        selection.Insert(addBeginComment)
        selection.NewLine()
        selection.Insert(description)


        For i = 0 To SelLineCount
            selection.LineDown()
            selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, False)
            selection.Insert("// ")
        Next


        selection.EndOfLine()
        selection.NewLine()
        selection.Insert(addEndComment)


    End Sub


End Module

2、开始做自己的工具条了:开始-自定义  


新建一个工具栏,如新建一个:A宏,重排命名,如下图,添加命令


这样就形成了一个名字为:A宏的工具条。

3、将工具条,添加到Visual的界面上

视图-工具栏,然后找到自己的刚刚定义的A宏工具条就可以了。

 


猜你喜欢

转载自blog.csdn.net/Simple_Man_Just/article/details/80371405