在敲代码的过程中类和函数都需要进行注释,但总是一遍一遍的复制粘贴觉得很是麻烦,终于找到了一个不错的解决方法:使用宏。
? ? 所谓宏,就是一些命令组织在一起,作为一个单独命令完成一个特定任务。在日常的办公环境中,不论是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.gsm-guard.netion
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.gsm-guard.net"
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.gsm-guard.net, Len(www.gsm-guard.net) - 2))
End Function
Sub HeaderFileTemplate()
If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
If UCase(Right(www.gsm-guard.net, 2)) = ".H" Then '头文件
Dim sGUID = GenGUIDString()
Dim sFile = FileString(www.gsm-guard.net)
Dim lens = 0
Dim strDesc = "/*******************************************************************************" + vbLf + _
"* 版权所有(C) " + Copyright() + vbLf + _
"* 文件名称 : " + www.gsm-guard.net + vbLf + _
"* 当前版本 : " + "1.0.0.1" + vbLf + _
"* 作 者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
"* 设计日期 : " + FormatDateTime(www.gsm-guard.net, 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.gsm-guard.net, 2))
Dim format2 = UCase(Right(www.gsm-guard.net, 4))
If format1 = ".C" Or format2 = ".CPP" Or format2 = ".CXX" Then '实现文件
Dim Descr = "/*******************************************************************************" + vbLf + _
"* 版权所有(C) " + Copyright() + vbLf + _
"* 文件名称 : " + www.gsm-guard.net + vbLf + _
"* 当前版本 : " + "1.0.0.1" + vbLf + _
"* 作 者 : " + AuthorName() + " (" + EMailAddress() + ")" + vbLf + _
"* 设计日期 : " + FormatDateTime(www.gsm-guard.net, 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.gsm-guard.netion = 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.gsm-guard.net, 1) + vbLf + _
"* 修改日期 修改人 修改内容" + vbLf + _
"*******************************************************************************/" + vbLf
ActiveDocument.Selection.text = Descr
End If
End If
End If
End Sub
End Module
? ? 。
VS2010中为类、函数代码添加版权注释信息
VS2010中为类、函数代码添加版权注释信息分为两步:
1.新建宏
在VS2010左侧找到Macro 资源管理器,找到已经存在的宏项目samples。
双击任一模块,进入Microsoft Visual Studio Macros。在下拉栏(图中红色标出)选择添加模块。
接着就可以对添加的模块进行编辑。代码可以在网上找到。下面会附出我自己的编辑后的代码。
编辑完成后记得保存。
2.绑定快捷键
工具---选项
依次操作。选择键盘,选择默认值,在“显示命令包含”输出相关信息快速定位添加的宏,按快捷键,分配,确定。
分配后的结果图。记得点击确定。
完成之后就可以测试效果了,下面是我的测试效果图
代码:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module FunctionNote
? ? Sub AddFunComment()
? ? ? ? Dim DocSel As EnvDTE.TextSelection
? ? ? ? DocSel = www.gsm-guard.netion
? ? ? ? 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.gsm-guard.net"
? ? ? ? DocSel.NewLine()
? ? ? ? DocSel.Text = "* Date: " + System.DateTime.Now.ToLongDateString()
? ? ? ? DocSel.NewLine()
? ? ? ? DocSel.Text = "*******************************************************************/"
? ? End Sub
End Module
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module HeadFileNote
Sub DocumentFileHeader()
Dim doc As Document
Dim docName As String
Dim companyName As String = "NERCITA"
Dim authorName As String = "Happy"
Dim copyrightText As String = String.Format("Copyright (c) {0} {1}. All rights reserved.", Date.Now.Year, companyName)
' 从程序中获得文件的名字
doc = www.gsm-guard.netDocument
'获得当前编辑类的名字
docName = www.gsm-guard.net
' 将添加焦点定位在文件首部
DTE.ActiveDocument.Selection.StartOfDocument()
' 添加一个版权说明
DTE.ActiveDocument.Selection.Text = "/***************************************************************/" '以String类型添加自己想要的符号、文字
DTE.ActiveDocument.Selection.NewLine()
' 添加一个空行
DTE.ActiveDocument.Selection.Text = "/* @ FileName: " + docName + ""
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "/* @ " + copyrightText
' 添加用户相关信息
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "/* @ Author: " + authorName + ""
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "/* @ Date: " + String.Format("{0:D}", www.gsm-guard.net) + ""
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "/* @ Description: "
DTE.ActiveDocument.Selection.Text = "*/"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "/***************************************************************/"
DTE.ActiveDocument.Selection.NewLine()
End Sub
End Module
参考链接:
http://www.gsm-guard.net/CookBlack/archive/2011/03/04/1971283.html#printSource
http://www.gsm-guard.net/view/8ae46fa5b0717fd5360cdc1a.html