创建过程:
1. 新建Macro工程
打开 菜单 -->工具--> 宏 --> 新建宏项目...,根据向导提示建立工程。
2. 编辑Macro工程
打开 菜单 -->工具--> 宏 -->宏资源管理器 ,在红资源管理器中选择新建的工程,修改Module名(右键),在Module名上双击,在打开的Macro IDE中进行编辑。
3. 在Module下,添加要实现的Macro,并实现(内容为下面的代码)。
4. 保存
5.在VS2008中,双击添加的Macro,将执行对应的Macro代码。
6. 建立快捷键
打开 菜单 --> 工具-->选项 --> 键盘 ,在列表框中选择刚才添加的Macro,然后在 按快捷键中输入快捷键,点击"分配" 。
7. 这时候,就可以直接使用快捷键来执行Macro 。
代码实现如下:
Imports
System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
'/*******************************************************************************
'* 版权所有(C) tusheng 2009
'* 文件名称 : MyCodeNote
'* 当前版本 : 1.0.0.1
'* 作 者 : 金朋轩 (jinpengxuan@126.com)
'* 设计日期 : 2009年7月29日
'* 内容摘要 : VS2008 Comment Macro
'*******************************************************************************/
Public Module MyCodeNote
Private Function Copyright()
Copyright = CStr(Date.Today.Year) +
"ToSheng.com All right reserved"
End Function
Private Function EMailAddress()
EMailAddress = "jinpengxuan@126.com"
End Function
Private Function AuthorName()
AuthorName = "金朋轩"
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, ","))
'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