一般来说代码文件都要有个文档头,带有版权声明和帮助信息等。我以前都是用CtrlC,CtrlV来做,后来研究了一下网上的资料,发现用宏来完成这个任务比较方便。
在工具菜单中选择宏->宏IDE,在宏编辑器中添加新项,选择模块,嗯,模块名字就叫做"AddDocumentHeader"吧。
编辑代码内容如下:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module AddDocumentHeader
Sub AddDocumentHeader()
Dim document As Document
document = DTE.ActiveDocument
document.Selection.StartOfDocument()
document.Selection.GotoLine(2, True)
Dim content As String = document.Selection.Text
'通过比较代码第二行版权信息字符串判断是否需要添加文件头.
Dim copyrightInformation As String = "Copyright (c) xwingyz(at)gmail.com. All right reserved."
Dim ms = " * " + copyrightInformation
Dim Found = String.Compare(content, ms)
If (Found <> 0) Then
document.Selection.StartOfDocument()
document.Selection.LineUp()
document.Selection.Text = "/* -----------------------------------------------------------------------------"
document.Selection.NewLine()
document.Selection.Text = copyrightInformation
document.Selection.NewLine()
document.Selection.NewLine()
document.Selection.NewLine()
document.Selection.Text = "$LastChangedBy$"
document.Selection.NewLine()
document.Selection.Text = "$LastChangedDate$"
document.Selection.NewLine()
document.Selection.Text = "$LastChangedRevision$"
document.Selection.NewLine()
document.Selection.Text = "-----------------------------------------------------------------------------"
document.Selection.NewLine()
document.Selection.Text = "/"
document.Selection.NewLine()
End If
End Sub
End Module
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module AddDocumentHeader
Sub AddDocumentHeader()
Dim document As Document
document = DTE.ActiveDocument
document.Selection.StartOfDocument()
document.Selection.GotoLine(2, True)
Dim content As String = document.Selection.Text
'通过比较代码第二行版权信息字符串判断是否需要添加文件头.
Dim copyrightInformation As String = "Copyright (c) xwingyz(at)gmail.com. All right reserved."
Dim ms = " * " + copyrightInformation
Dim Found = String.Compare(content, ms)
If (Found <> 0) Then
document.Selection.StartOfDocument()
document.Selection.LineUp()
document.Selection.Text = "/* -----------------------------------------------------------------------------"
document.Selection.NewLine()
document.Selection.Text = copyrightInformation
document.Selection.NewLine()
document.Selection.NewLine()
document.Selection.NewLine()
document.Selection.Text = "$LastChangedBy$"
document.Selection.NewLine()
document.Selection.Text = "$LastChangedDate$"
document.Selection.NewLine()
document.Selection.Text = "$LastChangedRevision$"
document.Selection.NewLine()
document.Selection.Text = "-----------------------------------------------------------------------------"
document.Selection.NewLine()
document.Selection.Text = "/"
document.Selection.NewLine()
End If
End Sub
End Module
保存,退出。
打开一个代码文件,选择工具菜单中宏->Macro资源管理器,选择刚刚建立的模块AddDocumentHeader右键菜单上选择运行,看看代码中是否自动添加了文件头?再执行一次看看是不是没有重复添加?不出意外的话一切OK。
下一步是要给这个宏分配一个快捷键,这样就更方便了。
找到工具菜单中选项->键盘选项, 在过滤框中输入AddDocumentHeader,看到你刚刚建立的宏了吧? 赶紧分配一个你喜欢快捷键吧。