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

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

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

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

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

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

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

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

 1 Public Sub FileSign()
 2         Dim DocSel As EnvDTE.TextSelection
 3         DocSel = DTE.ActiveDocument.Selection
 4 
 5         '活动点移到文件开头
 6         DTE.ActiveDocument.Selection.StartOfDocument()
 7 
 8         If DocSel.FindPattern("Last modified", vsFindOptions.vsFindOptionsRegularExpression) Then
 9             '找到Last modified字符串,说明已经添加过作者信息,只更新Last modified和Filename信息
10             DocSel.SelectLine()
11             DocSel.Delete()
12             DocSel.SelectLine()
13             DocSel.Delete()
14             DocSel.Text = "//  Last modified : " + Date.Today.ToString("yyyy-MM-dd") + " " + TimeOfDay.Hour.ToString("00") + ":" + TimeOfDay.Minute.ToString("00")
15             DocSel.NewLine()
16             DocSel.Text = "//  Filename      : " + DTE.ActiveDocument.Name
17             DocSel.NewLine()
18         Else
19             '没有找到Last modified字符串,添加全部信息
20             DTE.ActiveDocument.Selection.StartOfDocument()
21             DocSel.Text = "//=================================================================="
22             DocSel.NewLine()
23             DocSel.Text = "//  Author        : vitah"
24             DocSel.NewLine()
25             DocSel.Text = "//  Mail          : linw1225@163.com"
26             DocSel.NewLine()
27             DocSel.Text = "//  Last modified : " + Date.Today.ToString("yyyy-MM-dd") + " " + TimeOfDay.Hour.ToString("00") + ":" + TimeOfDay.Minute.ToString("00")
28             DocSel.NewLine()
29             DocSel.Text = "//  Filename      : " + DTE.ActiveDocument.Name
30             DocSel.NewLine()
31             DocSel.Text = "//  Description   :"
32             DocSel.NewLine()
33             DocSel.Text = "//"
34             DocSel.NewLine()
35             DocSel.Text = "//=================================================================="
36             DocSel.NewLine()
37             DocSel.NewLine()
38         End If
39 
40         DocSel.MoveToLineAndOffset(7, 5)  '活动点移动到Description处,填写描述信息
41     End Sub
FileSign
(2)FuncSign函数:添加函数注释
 1  Public Sub FuncSign()
 2         Dim DocSel As EnvDTE.TextSelection
 3         DocSel = DTE.ActiveDocument.Selection
 4         DocSel.NewLine()
 5         DocSel.Text = "//=================================================================="
 6         DocSel.NewLine()
 7         DocSel.Text = "//  Function       : "
 8         DocSel.NewLine()
 9         DocSel.Text = "//  Description    : "
10         DocSel.NewLine()
11         DocSel.Text = "//  Calls          : "
12         DocSel.NewLine()
13         DocSel.Text = "//  Called By      : "
14         DocSel.NewLine()
15         DocSel.Text = "//  Table Accessed : "
16         DocSel.NewLine()
17         DocSel.Text = "//  Table Updated  : "
18         DocSel.NewLine()
19         DocSel.Text = "//  Input          : "
20         DocSel.NewLine()
21         DocSel.Text = "//  Output         : "
22         DocSel.NewLine()
23         DocSel.Text = "//  Return         : "
24         DocSel.NewLine()
25         DocSel.Text = "//  Others         : "
26         DocSel.NewLine()
27         DocSel.Text = "//=================================================================="
28     End Sub
FuncSign

添加完成后保存:

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

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

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

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

注:

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

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

附:

(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

原文地址:https://www.cnblogs.com/vitah/p/3690442.html