您的位置:首页 > 其它

vc2008 中使用宏来添加注释的步骤

2014-04-10 22:44 447 查看
在VS2008里面通过宏可以记录下注释信息,产生一个注释文件,每次需要的时候直接增加到类文件里面即可,方法如下:

在vc中:tools->Macros->MacrosExplores 打开宏浏览器

然后新建个宏工程 宏文件(Module1)名称修改为COMMENT 然后 将下列代码加入

然后打开 工具->宏->宏资源管理器 双击宏名称就可以使用了。

'/**
'* @file COMMENT.DSM
'* @brief 添加文件头注释、类注释、函数注释、模块注释等。
'* @author lishaojie
'* @date 2014-04-10 21:45:00
'* @version 0.1
'* \pre
'* \details
'* \copyright: All rights reserved
'* \email:
'* \company:
'* \modification:
'*/
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

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

'生成Doxygen样式的函数注释
Public Sub FunctionDescription()

'判断所选择的行
Dim StartLine, endLine, Temp, tmpLine, Header, Reti, Loc, RetTp, Loc2, fcName, iPrm, iPrmA, prms, ParamArr, Last
StartLine = ActiveDocument.Selection.TopLine
endLine = ActiveDocument.Selection.BottomLine
If endLine < StartLine Then
Temp = StartLine
StartLine = endLine
endLine = Temp
End If

'如果行数大于,则将各行的字符串合成一个字符串
tmpLine = StartLine
Do While tmpLine <= endLine
ActiveDocument.Selection.GoToLine(tmpLine)
ActiveDocument.Selection.SelectLine()
Header = Header & StripTabs(Trim(ActiveDocument.Selection.text))
tmpLine = tmpLine + 1
Loop

'把回车换成空格
Header = Replace(Header, vbCrLf, " ")

ActiveDocument.Selection.GoToLine(StartLine)

If Header <> "" Then
Reti = InStr(Header, " ")
Loc = InStr(Header, "(")
If Reti < Loc Then
RetTp = Left(Header, Reti)
Header = Right(Header, Len(Header) - Reti)
End If

Loc = InStr(Header, "(") - 1
Loc2 = InStr(Header, ")")
If Loc > 0 And Loc2 > 0 Then
fcName = Left(Header, Loc)
Header = Right(Header, Len(Header) - Len(fcName))

Trim(fcName)

'得到函数名称
Do While InStr(fcName, " ") <> 0
RetTp = RetTp + Left(fcName, InStr(fcName, " "))
fcName = Right(fcName, Len(fcName) - InStr(fcName, " "))
Loop

'如果函数名称第一个字符为"*"或"&",则做为返回值最后一个字符
If InStr(fcName, "*") = 1 Then
RetTp = RTrim(RetTp) + "*"
fcName = LTrim(Right(fcName, Len(fcName) - 1))
End If
If InStr(fcName, "&") = 1 Then
RetTp = RTrim(RetTp) + "&"
fcName = LTrim(Right(fcName, Len(fcName) - 1))
End If

'对返回值进行处理
'去掉virtual
If InStr(RetTp, "virtual") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("virtual")))
End If

'去掉inline
If InStr(RetTp, "inline") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("inline")))
End If

'去掉static
If InStr(RetTp, "static") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("static")))
End If

iPrm = 0
iPrmA = 0
prms = Header

Do While InStr(prms, ",") <> 0
iPrm = iPrm + 1
prms = Right(prms, Len(prms) - InStr(prms, ","))
Loop

If iPrm > 0 Then
iPrm = iPrm + 1
iPrmA = iPrm
ReDim ParamArr(iPrm)
Do While InStr(Header, ",") <> 0
ParamArr(iPrm) = Left(Header, InStr(Header, ",") - 1)

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

If InStr(ParamArr(iPrm), ")") <> 0 Then
ParamArr(iPrm) = Left(ParamArr(iPrm), InStr(ParamArr(iPrm), ")") - 1)
Trim(ParamArr(iPrm))
End If
Else
ReDim ParamArr(1)
Header = Right(Header, Len(Header) - 1)
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
If ParamArr(1) = "void" Then
iPrmA = 0
End If
End If
End If

If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If

ActiveDocument.Selection.text = "/** "
ActiveDocument.Selection.NewLine()

'判断是构造函数还是析构函数
If Len(Trim(RetTp)) > 0 Then
ActiveDocument.Selection.text = "* @brief " + fcName + " "
Else
'为构造函数
If InStr(fcName, "~") <> 0 Then
ActiveDocument.Selection.text = "* @brief " + "Destructor for " + Right(fcName, Len(fcName) - 1) + "."
'为析构函数
Else
ActiveDocument.Selection.text = "* @brief " + "Constructor for " + fcName + "."
End If
End If

ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."

Last = iPrmA
Do While iPrmA <> 0
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 iPrmA = Last And Last <> 1 Then
ParamArr(iPrmA) = Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - 1)
End If
ActiveDocument.Selection.NewLine()

'首先判断参数列表中有没有'='号,如果有,则等号左边为参数名,右边为默认值。
Dim defautValue
If InStr(ParamArr(iPrmA), "=") <> 0 Then
defautValue = LTrim(Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - InStr(ParamArr(iPrmA), "=")))
ParamArr(iPrmA) = RTrim(Left(ParamArr(iPrmA), InStr(ParamArr(iPrmA), "=") - 1))
End If

Do While InStr(defautValue, " ") <> 0
defautValue = Right(defautValue, Len(defautValue) - InStr(defautValue, " "))
Loop

Do While InStr(ParamArr(iPrmA), " ") <> 0
ParamArr(iPrmA) = Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - InStr(ParamArr(iPrmA), " "))
Loop

'如果形参形如std::string &name时,应该将引用符号放到前边
If InStr(ParamArr(iPrmA), "*") = 1 Or InStr(ParamArr(iPrmA), "&") = 1 Then
ParamArr(iPrmA) = LTrim(Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - 1))
End If

If Len(Trim(defautValue)) > 0 Then
ActiveDocument.Selection.text = "* @param[in] " + LTrim(ParamArr(iPrmA)) + " Defaults to " + Trim(defautValue) + "."
Else
ActiveDocument.Selection.text = "* @param[in] " + LTrim(ParamArr(iPrmA)) + " "
End If
iPrmA = iPrmA - 1
Loop

ActiveDocument.Selection.NewLine()
If Len(Trim(RetTp)) > 0 And Trim(RetTp) <> "void" Then
ActiveDocument.Selection.text = "* @return " + RetTp + " "
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "*/"
Else
MsgBox("It is possible that the function you are trying to work with has a syntax error.")
End If
End If
End Sub

'生成doxygen样式的简要注释
Public Sub BriefDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @brief  */"
End Sub
'生成doxygen样式的公开变量的注释
Public Sub MemberDescription()
ActiveDocument.Selection.text = ActiveDocument.Selection.text + " /**<  */"
End Sub
'生成doxygen样式的一般通用的注释
Public Sub DetailDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End Sub
'生成doxygen样式的一般通用的注释
Public Sub Define()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#define "
End Sub
'生成doxygen样式的一般通用的注释
Public Sub Include()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#include "
End Sub
'生成doxygen样式的一般通用的注释
Public Sub TypedefStruct()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "typedef struct"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "}TSP_PACKED _Struct;"
ActiveDocument.Selection.NewLine()
End Sub

'生成doxygen样式的一般通用的注释
Public Sub TypedefEnum()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "typedef Enum"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "}_Enum;"
ActiveDocument.Selection.NewLine()
End Sub

'生成doxygen样式的文件描述
Public Sub FileDescription()
If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @file " + ActiveDocument.Name
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @author ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @date "
ActiveDocument.Selection.text = DateTime.Today + " " + TimeOfDay
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @version "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \pre ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \details ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \copyright: *** All rights reserved."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \email: ***@***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \company: http://" ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \modification:"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \Write modifications here."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End Sub

'生成doxygen样式的新文件描述
Public Sub NewFileDescription()
If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @file " + ActiveDocument.Name
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @author ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @date "
ActiveDocument.Selection.text = DateTime.Today + " " + TimeOfDay
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @version "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \pre ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \details ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \copyright: *** All rights reserved."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \email: ***@***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \company: http://" ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \modification:"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* \Write modifications here."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
If InStr(ActiveDocument.Name, ".") > 0 Then
If Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "h" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "hpp" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "hh" Then
Dim def
def = "_" + UCase(Left(ActiveDocument.Name, InStr(ActiveDocument.Name, ".") - 1) _
+ "_" + Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")))
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#ifndef " + def
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#define " + def
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#endif "
ActiveDocument.Selection.NewLine()
End If
If Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "c" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "cpp" Then
Dim def
def = "#include " + Chr(34) + Left(ActiveDocument.Name, InStr(ActiveDocument.Name, ".")) + "h" + Chr(34)
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = def
ActiveDocument.Selection.NewLine()
End If
End If
End Sub

'生成doxygen样式的项目编号描述
Public Sub ItemDescription()

ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* - "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* - "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"

End Sub

'生成doxygen样式模块描述
Sub ModuleDescription()

ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @defgroup "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @} */ "
End Sub

'生成doxygen样式类描述
Sub ClassDescription()
Dim className
Dim StartLine
className = ActiveDocument.Selection.text
If Len(className) <= 0 Then
MsgBox("Please select the class name")
Else
StartLine = ActiveDocument.Selection.TopLine
ActiveDocument.Selection.GoToLine(StartLine)

If StartLine > 1 Then
ActiveDocument.Selection.MoveTo(StartLine - 1, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If

If InStr(className, "class") > 0 Then
className = LTrim(Right(className, Len(className) - Len("class")))
End If
If InStr(className, ":") > 0 Then
className = Trim(Left(className, InStr(className, ":") - 1))
Else
className = Trim(className)
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @class " + className
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End If
End Sub

'生成doxygen样式组描述
Sub GroupDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @name "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @} */"
End Sub
End Module


PS:

VS2008注释宏不能运行的解决办法

搜索 文件 vsmsvr.exe.config ( 默认C盘安装的路径为 C:\Program Files\Common Files\microsoft shared\VSA\9.0\VsaEnv)。。

打开这个文件在里面的runtime节点下增加 <AllowDComReflection enabled="1"/> 或<AllowDComReflection enabled="true"/> 一句,如下文件内容。。

<?xml version ="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
<runtime>
<AllowDComReflection enabled="1"/>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EnvDTE" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="7.0.3300.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.net>
<settings>
<ipv6 enabled="true" />
</settings>
</system.net>
</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: