您的位置:首页 > 其它

VS2010利用宏快速添加注释(函数描述,修改注释等)

2016-01-05 10:29 495 查看
在敲代码的过程中类和函数都需要进行注释,但总是一遍一遍的复制粘贴觉得很是麻烦,终于找到了一个不错的解决方法:使用宏。

所谓宏,就是一些命令组织在一起,作为一个单独命令完成一个特定任务。在日常的办公环境中,不论是Office还是Foxmail以及我们所使用的VS甚至输入法都具有宏的功能。VS2010中的宏,不仅可以录制模块、还可以录制类和代码文件。通过设置编辑宏,然后为设置好的宏添加特定的快捷键,就可以在VS2010代码编辑器中任何位置非常方便的添加设定的注释块。实现过程如下:



1、打开“工具”→“宏”→“宏IDE”,进入以下界面,右击“MyMacros”,添加模块


命名模块:




2、添加代码并保存

双击所添加的模块,进入编辑状态,添加如下代码:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module FunctionNotes
    Sub DocumentFileHeader()

        Dim DocSel As EnvDTE.TextSelection
        DocSel = DTE.ActiveDocument.Selection
        DocSel.NewLine()
        DocSel.Text = "/************************************************"
        DocSel.NewLine()
        DocSel.Text = "* &name:"
        DocSel.NewLine()
        DocSel.Text = "* &function:"
        DocSel.NewLine()
        DocSel.Text = "* ¶m[in]:"
        DocSel.NewLine()
        DocSel.Text = "* &return:"
        DocSel.NewLine()
        DocSel.Text = "* &author:rk"
        DocSel.NewLine()
        DocSel.Text = "* &version:V1.0"
        DocSel.NewLine()
        DocSel.Text = "* &date:" + System.DateTime.Now.ToLongDateString
        DocSel.NewLine()
        DocSel.Text = "************************************************/"

    End Sub

End Module
注意:设置快捷键之前,最好先运行一下宏,确定宏的能正确执行后再添加快捷键,

可以在 “工具”→“宏”→“Macro资源管理器”中找到刚添加的FunctionNotes下的DocumentFileHeader,右键运行一下

看一看有没有效果。

如果没有效果,可能是这个原因:点击打开链接

3、设置快捷键

打开“工具”→“选项”,选择“键盘”,进行如下设置




4、效果
<span style="color:#009900;">/************************************************
* &name:
* &function:
* ¶m[in]:
* &return:
* &author:rk
* &version:V1.0
* &date:2016年1月5日 星期二
************************************************/</span>


这个只是一个比较简单的宏,用来交给大家如何使用,如果有兴趣写出功能更加强大的宏,

以下代码大家可以参考:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module Module1
    Private Function Copyright()
        Copyright = CStr(Date.Today.Year) + "科技 All right reserved"
    End Function

    Private Function EMailAddress()
        EMailAddress = "tangxingqt@163.com"
    End Function

    Private Function AuthorName()
        AuthorName = "rk"
    End Function

    Function ProductName()
        ProductName = ""
    End Function

    Private Function GenGUIDString() As String
        Dim sGUID As String
        sGUID = System.Guid.NewGuid.ToString()
        sGUID = UCase(sGUID.Replace("-", "_"))
        GenGUIDString = sGUID
    End Function

    Private Function FileString(ByVal filename As String) As String
        FileString = UCase(filename.Replace(".", "_"))
        UCase(Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 2))
    End Function

    Sub HeaderFileTemplate()
        If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
            If UCase(Right(ActiveDocument.Name, 2)) = ".H" Then  '头文件
                Dim sGUID = GenGUIDString()
                Dim sFile = FileString(ActiveDocument.Name)
                Dim lens = 0
                Dim strDesc = "/*******************************************************************************" + vbLf + _
                              "* 版权所有(C) " + Copyright() + vbLf + _
                              "* 文件名称 : " + ActiveDocument.Name + vbLf + _
                              "* 当前版本 : " + "1.0.0.1" + vbLf + _
                              "* 作    者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
                              "* 设计日期 : " + FormatDateTime(Date.Today, 1) + vbLf + _
                              "* 内容摘要 : " + vbLf + _
                              "* 修改记录 : " + vbLf + _
                              "* 日    期  版    本  修改人   修改摘要" + vbLf + vbLf + _
                              "********************************************************************************/" + vbLf + _
                              "" + vbLf + _
                              "/**********************************  头文件 ************************************/" + vbLf + _
                              "" + vbLf + _
                             "/********************************** 常量和宏 **********************************/" + vbLf + _
                              "" + vbLf + _
                              "/********************************** 数据类型 **********************************/" + vbLf + _
                              "" + vbLf + _
                              "/********************************** 函数声明 **********************************/" + vbLf + _
                              "" + vbLf + _
                              "/********************************** 类定义 ***********************************/" + vbLf + _
                              "" + vbLf + _
                ActiveDocument.Selection.StartOfDocument(0)
                ActiveDocument.Selection.text() = strDesc
            End If
        End If
    End Sub
    Sub ImplFileTemplate()
        If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
            Dim format1 = UCase(Right(ActiveDocument.Name, 2))
            Dim format2 = UCase(Right(ActiveDocument.Name, 4))
            If format1 = ".C" Or format2 = ".CPP" Or format2 = ".CXX" Then  '实现文件
                Dim Descr = "/*******************************************************************************" + vbLf + _
                              "* 版权所有(C) " + Copyright() + vbLf + _
                              "* 文件名称 : " + ActiveDocument.Name + vbLf + _
                              "* 当前版本 : " + "1.0.0.1" + vbLf + _
                              "* 作    者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
                              "* 设计日期 : " + FormatDateTime(Date.Today, 1) + vbLf + _
                              "* 内容摘要 : " + vbLf + _
                              "* 修改记录 : " + vbLf + _
                              "* 日    期  版    本  修改人   修改摘要" + vbLf + vbLf + _
                              "********************************************************************************/" + vbLf + _
                            "/**************************** 条件编译选项和头文件 ****************************/" + vbLf + _
                            "" + vbLf + _
                            "/********************************** 宏、常量 **********************************/" + vbLf + _
                            "" + vbLf + _
                            "/********************************** 数据类型 **********************************/" + vbLf + _
                            "" + vbLf + _
                            "/************************************ 变量 ************************************/" + vbLf + _
                            "" + vbLf + _
                            "/********************************** 函数实现 **********************************/" + vbLf + _
                            "" + vbLf + _
                            "/*********************************** 类实现 ***********************************/" + vbLf + _
                            "" + vbLf

                ActiveDocument.Selection.StartOfDocument(0)
                ActiveDocument.Selection.text = Descr
            End If
        End If
    End Sub

    Dim ParamArr()
    Function StripTabs(ByVal MyStr)
        Do While InStr(MyStr, vbTab) <> 0
            MyStr = Right(MyStr, Len(MyStr) - InStr(MyStr, vbTab))
        Loop
        StripTabs = Trim(MyStr)
    End Function

    Sub FunctionDesc()
        Dim retTp
        Dim Reti

        If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
            Dim Header = Trim(ActiveDocument.Selection.text)

            'Get the function return type.
            If Header <> "" Then
                Reti = InStr(Header, " ")
                Dim Loc = InStr(Header, "(")
                If Reti < Loc Then
                    retTp = StripTabs(Left(Header, Reti))
                    Header = Right(Header, Len(Header) - Reti)
                End If

                'Get the function name.
                Loc = InStr(Header, "(") - 1
                Dim Loc2 = InStr(Header, ")")
                If Loc > 0 And Loc2 > 0 Then 'make sure there is a '(' and a ')'
                    Dim fcName = Left(Header, Loc)
                    Header = Right(Header, Len(Header) - Len(fcName))

                    'Do we have storage type on the return type?
                    Trim(fcName)
                    If InStr(fcName, " ") <> 0 Then
                        retTp = retTp + Left(fcName, InStr(fcName, " "))
                        fcName = Right(fcName, Len(fcName) - InStr(fcName, " "))
                    End If

                    'Get the function parameters.
                    Dim iPrm = 0
                    Dim iPrmA = 0
                    Dim prms = Header

                    'Count the number of parameters. 
                    Do While InStr(prms, ",") <> 0
                        iPrm = iPrm + 1
                        prms = Right(prms, Len(prms) - InStr(prms, ","))
                    Loop

                    'Store the parameter list in the array.
                    If iPrm > 0 Then  ' If multiple params.
                        iPrm = iPrm + 1
                        iPrmA = iPrm
                        ReDim ParamArr(iPrm)
                        Do While InStr(Header, ",") <> 0
                            ParamArr(iPrm) = Left(Header, InStr(Header, ",") - 1)
                            'Remove brace from first parameter.
                            If InStr(ParamArr(iPrm), " (") <> 0 Then
                                ParamArr(iPrm) = Right(ParamArr(iPrm), _
                                  Len(ParamArr(iPrm)) - InStr(ParamArr(iPrm), " ("))
                                Trim(ParamArr(iPrm))
                            End If
                            Header = Right(Header, Len(Header) - InStr(Header, ","))
                            iPrm = iPrm - 1
                        Loop
                        ParamArr(iPrm) = Header
                        'Remove trailing brace from last parameter.
                        If InStr(ParamArr(iPrm), ")") <> 0 Then
                            ParamArr(iPrm) = Left(ParamArr(iPrm), _
                              InStr(ParamArr(iPrm), ")") - 1)
                            Trim(ParamArr(iPrm))
                        End If
                    Else 'Possibly one param.
                        ReDim ParamArr(1)
                        Header = Right(Header, Len(Header) - 1) ' Strip the first brace.
                        Trim(Header)
                        ParamArr(1) = StripTabs(Header)
                        If InStr(ParamArr(1), ")") <> 1 Then
                            ParamArr(1) = Left(ParamArr(1), InStr(ParamArr(1), ")") - 1)
                            Trim(ParamArr(1))
                            iPrmA = 1
                        End If
                    End If

                    'Position the cursor one line above the selected text.
                    ActiveDocument.Selection.LineUp()
                    ActiveDocument.Selection.LineDown()
                    ActiveDocument.Selection.StartOfLine()
                    'ActiveDocument.Selection = vbLf

                    Dim Descr = "/*******************************************************************************" + vbLf + _
                            "* 函数名称 : " + fcName + vbLf + _
                      "* 功能描述 : "

                    'Print the parameter list. 
                    Dim Last = iPrmA
                    Do While iPrmA <> 0
                        'Remove a line feed from any of the arguments.
                        If InStr(ParamArr(iPrmA), vbLf) <> 0 Then
                            ParamArr(iPrmA) = Right(ParamArr(iPrmA), _
                              (Len(ParamArr(iPrmA)) - _
                              InStr(ParamArr(iPrmA), vbLf)))
                            Trim(ParamArr(iPrmA))
                        End If
                        ParamArr(iPrmA) = StripTabs(ParamArr(iPrmA))
                        'If there are 2+ parameters, the first parameter will 
                        'have a '(' prepended to it, remove it here:
                        If iPrmA = Last And Last <> 1 Then
                            ParamArr(iPrmA) = Right(ParamArr(iPrmA), _
                            Len(ParamArr(iPrmA)) - 1)
                        End If
                        Descr = Descr + vbLf + "* 参  数 : " + _
                          ParamArr(iPrmA)
                        iPrmA = iPrmA - 1
                    Loop
                    Descr = Descr + vbLf + _
                           "* 返 回 值 : " + retTp + vbLf + _
                        "* 作  者 : " + AuthorName() + vbLf + _
                        "* 设计日期 : " + FormatDateTime(Date.Today, 1) + vbLf + _
                           "* 修改日期     修改人    修改内容" + vbLf + _
                           "*******************************************************************************/" + vbLf
                    ActiveDocument.Selection.text = Descr
                End If
            End If
        End If
    End Sub

End Module


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: