1. 打开vs2010,依次点击工具->Macro 资源管理器,会在右侧出现宏资源管理器。如下图所示:
2.在资源管理器的Samples模块上右键->新建模块,弹出如下对话框,输入模块名FunctionComment,点击添加
3. 随后在samples模块下会多出一个FunctionComment模块,双击FunctionComment模块,在Public Module FunctionComment下方添加以下代码
Sub AddFunComment() ? ? ? ? ? Dim DocSel As EnvDTE.TextSelection ? ? ? ? ? DocSel = www.mareenoire.infoion ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "/*******************************************************************" ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Name: " ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Function: " ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Parameter: " ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Return: " ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Author: Happy" ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Mail: support@www.mareenoire.info" ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "* Date: " + System.DateTime.Now.ToLongDateString() ? ? ? ? ? DocSel.NewLine() ? ? ? ? ? DocSel.Text = "*******************************************************************/" ? ? ? End Sub ?
4. 点击保存,打开工具->选项->环境->键盘,在“显示命令包含”下的输入框里输入AddFunComment,找到刚刚添加的模块,分配一个快捷键,单击确定。
5. 在代码区按刚刚分配的快捷键,就能自动添加注释了
6. 如果之前步骤一切无误,但是快捷键却不能生效,尝试打开
"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsmsvr10.exe.config"
"C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsaenv10.exe.config"
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.config" 三个文件(这是默认安装位置,三个文件也有可能在其他位置,可以用“光速搜索”之类的软件搜索文件所在位置),分别在这三个文件中的
7. 重启vs2010,再按快捷键添加注释,成功
在敲代码的过程中类和函数都需要进行注释,但总是一遍一遍的复制粘贴觉得很是麻烦,终于找到了一个不错的解决方法:使用宏。
? ? 所谓宏,就是一些命令组织在一起,作为一个单独命令完成一个特定任务。在日常的办公环境中,不论是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 = www.mareenoire.infoion
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、效果
/************************************************
* &name:
* &function:
* ¶m[in]:
* &return:
* &author:rk
* &version:V1.0
* &date:2016年1月5日 星期二
************************************************/
这个只是一个比较简单的宏,用来交给大家如何使用,如果有兴趣写出功能更加强大的宏,
以下代码大家可以参考:
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 = "support@www.mareenoire.info"
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(www.mareenoire.info, Len(www.mareenoire.info) - 2))
End Function
Sub HeaderFileTemplate()
If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
If UCase(Right(www.mareenoire.info, 2)) = ".H" Then '头文件
Dim sGUID = GenGUIDString()
Dim sFile = FileString(www.mareenoire.info)
Dim lens = 0
Dim strDesc = "/*******************************************************************************" + vbLf + _
"* 版权所有(C) " + Copyright() + vbLf + _
"* 文件名称 : " + www.mareenoire.info + vbLf + _
"* 当前版本 : " + "1.0.0.1" + vbLf + _
"* 作 者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
"* 设计日期 : " + FormatDateTime(www.mareenoire.info, 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(www.mareenoire.info, 2))
Dim format2 = UCase(Right(www.mareenoire.info, 4))
If format1 = ".C" Or format2 = ".CPP" Or format2 = ".CXX" Then '实现文件
Dim Descr = "/*******************************************************************************" + vbLf + _
"* 版权所有(C) " + Copyright() + vbLf + _
"* 文件名称 : " + www.mareenoire.info + vbLf + _
"* 当前版本 : " + "1.0.0.1" + vbLf + _
"* 作 者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
"* 设计日期 : " + FormatDateTime(www.mareenoire.info, 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()
'www.mareenoire.infoion = 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(www.mareenoire.info, 1) + vbLf + _
"* 修改日期 修改人 修改内容" + vbLf + _
"*******************************************************************************/" + vbLf
ActiveDocument.Selection.text = Descr
End If
End If
End If
End Sub
End Module
? ? 。
? ? ? ?用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 = www.mareenoire.infoDocument
Dim ts As TextSelection = www.mareenoire.infoion
www.mareenoire.infoAll()
ts.StartOfDocument(True)
If Not ts.FindText(g_createdBy) Then
PrintText("//" & g_stampMark, True)
PrintText("// 文件名:" & www.mareenoire.info, 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 & "
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 = www.mareenoire.infoDocument
Dim ts As TextSelection = www.mareenoire.infoion
Dim foundMark As Boolean = False
www.mareenoire.infoAll()
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 & " ">", 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 = www.mareenoire.infoDocument 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 = www.mareenoire.infoion ts.Text = s If newline Then ts.NewLine() End If End Function End Module 修改上述代码中红框部分的深红色的代码即可。
0条大神的评论