VS2010中编写宏添加作者信息与函数注释

  这里所说的宏是指通过一系列键盘组合键和可以插入自定义内容。下面介绍怎么编写一个自己的宏:

1、在Visual Studio 2010中按Alt+F11打开宏IDE:

VS2010中编写宏添加作者信息与函数注释

2、打开后选择添加模块:

VS2010中编写宏添加作者信息与函数注释

3、在弹出的窗口中输入名称后确定添加:

VS2010中编写宏添加作者信息与函数注释

4、出现如下页面即可进行编辑:

VS2010中编写宏添加作者信息与函数注释

5、在Public Module MyInformation中添加如下代码:

(1)FileSign函数:添加作者信息

 Public Sub FileSign()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection '活动点移到文件开头
DTE.ActiveDocument.Selection.StartOfDocument() If DocSel.FindPattern("Last modified", vsFindOptions.vsFindOptionsRegularExpression) Then
'找到Last modified字符串,说明已经添加过作者信息,只更新Last modified和Filename信息
DocSel.SelectLine()
DocSel.Delete()
DocSel.SelectLine()
DocSel.Delete()
DocSel.Text = "// Last modified : " + Date.Today.ToString("yyyy-MM-dd") + " " + TimeOfDay.Hour.ToString("") + ":" + TimeOfDay.Minute.ToString("")
DocSel.NewLine()
DocSel.Text = "// Filename : " + DTE.ActiveDocument.Name
DocSel.NewLine()
Else
'没有找到Last modified字符串,添加全部信息
DTE.ActiveDocument.Selection.StartOfDocument()
DocSel.Text = "//=================================================================="
DocSel.NewLine()
DocSel.Text = "// Author : vitah"
DocSel.NewLine()
DocSel.Text = "// Mail : linw1225@163.com"
DocSel.NewLine()
DocSel.Text = "// Last modified : " + Date.Today.ToString("yyyy-MM-dd") + " " + TimeOfDay.Hour.ToString("") + ":" + TimeOfDay.Minute.ToString("")
DocSel.NewLine()
DocSel.Text = "// Filename : " + DTE.ActiveDocument.Name
DocSel.NewLine()
DocSel.Text = "// Description :"
DocSel.NewLine()
DocSel.Text = "//"
DocSel.NewLine()
DocSel.Text = "//=================================================================="
DocSel.NewLine()
DocSel.NewLine()
End If DocSel.MoveToLineAndOffset(, ) '活动点移动到Description处,填写描述信息
End Sub

FileSign

(2)FuncSign函数:添加函数注释
  Public Sub FuncSign()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.NewLine()
DocSel.Text = "//=================================================================="
DocSel.NewLine()
DocSel.Text = "// Function : "
DocSel.NewLine()
DocSel.Text = "// Description : "
DocSel.NewLine()
DocSel.Text = "// Calls : "
DocSel.NewLine()
DocSel.Text = "// Called By : "
DocSel.NewLine()
DocSel.Text = "// Table Accessed : "
DocSel.NewLine()
DocSel.Text = "// Table Updated : "
DocSel.NewLine()
DocSel.Text = "// Input : "
DocSel.NewLine()
DocSel.Text = "// Output : "
DocSel.NewLine()
DocSel.Text = "// Return : "
DocSel.NewLine()
DocSel.Text = "// Others : "
DocSel.NewLine()
DocSel.Text = "//=================================================================="
End Sub

FuncSign

添加完成后保存:

VS2010中编写宏添加作者信息与函数注释

6、保存后可以关闭宏IDE,进入Visual Studio 2010主界面—>工具—>选项—>环境—>键盘项,进入如下页面:

VS2010中编写宏添加作者信息与函数注释

7、接着可以为刚才编写的宏映射键盘快捷键(在此只演示添加作者信息的函数FileSign的设置操作,函数FuncSign设定与之类似):

VS2010中编写宏添加作者信息与函数注释

输入快捷键后,点击分配按钮,若快捷键没有被占用则下面的“快捷键的当前使用对象”框内容为空,可以点击确定完成宏的映射设置;

8、完成后,即可验证本次宏的编写是否正确。

注:

本文中的代码适用于C/C++文件,且注释必须是每行行首添加“//”,若是在首行行首和末行行末各添加“/*”和“*/”会出现如下问题:

当代码文件的第一行不是空白行时,会出现如下错误;若代码文件第一行为空,则显示正常。

VS2010中编写宏添加作者信息与函数注释

附:

(1)部分图片引用自CSDN郗晓勇的博客:http://blog.csdn.net/beijiguangyong/article/details/6371504

(2)MSDN中关于宏的一些介绍:http://msdn.microsoft.com/zh-cn/library/b4c73967(v=vs.100).aspx

(3)MSDN中关于控制代码编辑器的介绍:http://msdn.microsoft.com/zh-cn/library/cwda3d81(v=vs.100).aspx

(4)MSDN中关于TextSelection接口的介绍:http://msdn.microsoft.com/zh-cn/library/EnvDTE.TextSelection_methods(v=vs.100).aspx

上一篇:PHP写日志函数


下一篇:3198: [Sdoi2013]spring【容斥原理+hash】