VS2010 如何使用宏添加注释

       用VS2010编写代码时可以使用宏命令快速的给类添加头注释(如下图:)。

           


首先添加一个宏:工具——宏——宏管理器——MyMacros——右键——添加一个新的Modules。

——双击新添加的Module进入编辑界面。



用下面的代码替换上图红框中的部分。

Imports EnvDTE
Imports System.Diagnostics
Public Module Stamps
    'DESCRIPTION: MQZ. Creates comment block 
    Dim g_strAuthor As String = "项目创建人"
    Dim g_strAuthorEmail As String = "邮箱名称"
    Dim g_strCompany As String = _
       "Copyright 2015, 公司名称. All rights reserved."
    Dim g_strTeam As String = "部门"
    Dim g_stampMark As String = "====================================================================="
    Dim g_createdBy As String = "// 创建人: "
    Dim g_updatedOn As String = "// 更新日期: "


    Sub StampHeaderCreation()
        'DESCRIPTION: Creates a creation comment block 
        'for the active Source files
        If (StampCheckDoc() = False) Then
            Exit Sub
        End If

        Dim doc As Document = DTE.ActiveDocument
        Dim ts As TextSelection = DTE.ActiveWindow.Selection

        ts.SelectAll()
        ts.StartOfDocument(True)

        If Not ts.FindText(g_createdBy) Then
            PrintText("//" & g_stampMark, True)
            PrintText("// 文件名:" & doc.Name, True)
            PrintText("//", True)
            ts.DeleteLeft(3)
            PrintText("// " & g_strCompany, True)
            PrintText("// " & g_strTeam, True)
            PrintText("// ", True)
            PrintText("// 描述:", True)
            PrintText("/*", True)
            PrintText("", True)
            PrintText("*/", True)
            PrintText("// 创建日期: " & CDate(Now) & "", True)
            PrintText(g_createdBy & g_strAuthor & " <mailto:" & g_strAuthorEmail & ">", True)
            PrintText("//" & g_stampMark, True)
            PrintText("", True)
            'ts.DeleteLeft(4)
        Else
            StampHeaderUpdate()
        End If

    End Sub
    Sub StampHeaderUpdate()
        'DESCRIPTION: Creates a update comment block 
        'for the active Source files
        If (StampCheckDoc() = False) Then
            Exit Sub
        End If

        Dim doc As Document = DTE.ActiveDocument
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        Dim foundMark As Boolean = False

        ts.SelectAll()
        ts.StartOfDocument(True)

        While ts.FindText(g_createdBy)
            ts.FindText(g_stampMark)
            foundMark = True
        End While

        While ts.FindText(g_updatedOn)
            ts.FindText(g_stampMark)
            foundMark = True
        End While

        If foundMark Then
            'ts.LineDown(False, 1)
            ts.EndOfLine()
            ts.NewLine()
        End If
        PrintText("// 更新日期: " & CDate(Now) & ". By: " & _
            g_strAuthor & " <mailto:" & g_strAuthorEmail & _
            ">", True)
        PrintText("// 描述:", True)
        PrintText("/*", True)
        PrintText("", True)
        PrintText("*/", True)
        PrintText("//" & g_stampMark, True)
        PrintText("", True)
        'ts.DeleteLeft(4)
    End Sub

    Function StampCheckDoc() As Boolean
        'DESCRIPTION: Creates a update comment block 
        'for the active Source files
        Dim doc As Document = DTE.ActiveDocument
        Dim badFile As Boolean = False
        Dim name As String

        If doc Is Nothing Then
            MsgBox("Please run when a text editor window is active.")
            Return False
        End If

        name = doc.Name.ToLower
        If name.EndsWith(".h") Or name.EndsWith(".cpp") _
                              Or name.EndsWith(".js") Then
            badFile = True
        End If

        If badFile Then
            MsgBox("Please run with a c/c++ or JavaScript file.")
            Return False
        End If

        Return True
    End Function

    Function PrintText(ByVal s As String, ByVal newline As Boolean)
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        ts.Text = s
        If newline Then
            ts.NewLine()
        End If
    End Function

End Module

修改上述代码中红框部分的深红色的代码即可。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值