VS 自动添加注释

From : http://hi.baidu.com/%C8%ED%BC%FE%CD%F8%C2%E7%B4%BF%BC%BC%CA%F5/blog/item/2baa5512c12b6a916438db40.html

现在大多数公司都规定程序员在程序文件的头部加上版权信息,这样每个人写的文件都可以区分开来,如果某个文件出现问题就可以快速的找到文件的创建人,用最短的时间来解决问题,常常是以下格式:
//======================================================================
//        All rights reserved
//
//        description :
//
//        created by User

//======================================================================    

有些人使用Copy和Paste的方式,这样即浪费时间,效果又不是很好,就说上面的时间你就无法去Paste,哈哈,下面我就教大家怎样去添加这些信息,使我们的代码更有个性.
1.在visual studio 2010的安装路径下
        如:[盘符]:\Program files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache
2.找到文件夹如图所示:
       
里面可以为各种语言进行修改.如果对WinForm中的类进行修改可以打开\CSharp\2052\Class.zip.其中\CSharp\2052\包括了所有WinForm文件类型的模板.
打开Class.zip里面有一个Class.cs文件,我们对其进行修改,当我们在WinForm中添加类文件的时候,类文件就会自动添加上我们的信息.
如下:
//======================================================================
//
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :
//
//        created by User at  $time$
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

$rootnamespace$为生成类的命名空间的名字,$safeitemrootname$为生成类的类名.
可以看到我们在版权信息中加入了$time$,它就可以直接给我们加入创建的时间.我们可以对\CSharp\2052中所有的模板进行修改,切忌不要轻易修改系统那些代码,以免影响我们的正常的使用.
对于做Web开发的人员来说可以在ItemTemplatesCache\Web\CSharp\2052里进行修改.
新建一个类文件就可以实现了插入我们自定义的版权信息.
//======================================================================
//
//        All rights reserved
//
//        filename :NewClass
//        description :
//
//        created by User at 

//
//======================================================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// NewClass 的摘要说明
/// </summary>
public class NewClass
{
    public NewClass()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
}
哈哈,是不是很酷呀!赶快个性一下你的代码吧!

这个是扩展参数

 

参数

描述

clrversion

当前系统CLR版本号

GUID [1-10]

生成全局唯一标识符,可以生成10 (例如:guid1)

itemname

打开添加新建项时输入的文件名称

machinename

当前机器的名称(:pc1)

registeredorganization

注册的组织名

rootnamespace

命名空间名

safeitemname

保存的文件名

time

当前系统时间,格式:DD/MM/YYYY 00:00:00.

userdomain

用户所在的域

username

当前系统用户名

year

当前系统时间 YYYY.

上面提供了部分的参数(希望有人继续补充),已经经本人在VS2010下测试,可以通过

/*小补充 - Start

另外,类库Web或Winform都是不同的(Web的在WebClass.zip下面的Class.cs中,各种不同的类型,在的位置也不同)

种类也各有不用,分别为Class.zip、Interface.zip、Form.zip等添加注释(我们是以Class.zip为例,也就是以类为基础)

此外:

这里$var$ 都是系统的变量,模板参数是要区分大小写的,大家需要注意。系统提供的可用的参数如下:

参数  说明
clrversion
 公共语言运行库 (CLR) 的当前版本。
 
GUID [1-10]
 用于替换项目文件中的项目 GUID 的 GUID。最多可以指定 10 个唯一的 GUID(例如,guid1))。
 
itemname
 用户在添加新项对话框中提供的名称。
 
machinename
 当前的计算机名称(例如,Computer01)。
 
projectname
 用户在新建项目对话框中提供的名称。
 
registeredorganization
 HKLM/Software/Microsoft/Windows NT/CurrentVersion/RegisteredOrganization 中的注册表项值。
 
rootnamespace
 当前项目的根命名空间。此参数用于替换正向项目中添加的项中的命名空间。
 
safeitemname
 用户在“添加新项”对话框中提供的名称,名称中移除了所有不安全的字符和空格。
 
safeprojectname
 用户在“新建项目”对话框中提供的名称,名称中移除了所有不安全的字符和空格。
 
time
 以 DD/MM/YYYY 00:00:00 格式表示的当前时间。
 
userdomain
 当前的用户域。
 
username
 当前的用户名。
 
year
 以 YYYY 格式表示的当前年份。

小补充 - End */

模板

/*****************************************************************************
*        filename :$safeitemrootname$
*        描述 :
*
*        创建者 User
*        CLR版本:           $clrversion$
*        新建项输入的名称:   $itemname$
*        机器名称:           $machinename$
*        注册组织名:         $registeredorganization$
*        命名空间名称:       $rootnamespace$
*        文件名:             $safeitemname$
*        创建系统时间:       $time$
*        创建年份:           $year$
*
/*****************************************************************************/

 

程序注释的重要性毋庸置疑,一个大型的项目通常情况下都是几个软件工程师合作出来的成果,假如该项目中的代码注释不全,那会给将来的维护者带来无穷无尽的隐患。

通用的办法是给自己工程里面的函数添加注释——使用宏。

1.打开Visual Studio 2008(2005一样适用)开发工具,单击“工具→宏→新建宏项目”,然后按照步骤建立注释宏,添加如下代码并保存。

2.打开 菜单 –> 工具–>选项 –> 键盘 ,在列表框中选择刚才添加的Macro,然后在 按快捷键中输入快捷键,点击”分配” 。

注释宏的代码如下:

 

Sub AddFunComment()
    Dim DocSel As EnvDTE.TextSelection
    DocSel = DTE.ActiveDocument.Selection
    DocSel.NewLine()
    DocSel.Text = "/*******************************************************************"
    DocSel.NewLine()
    DocSel.Text = "* 函数名称:"
    DocSel.NewLine()
    DocSel.Text = "* 功    能:"
    DocSel.NewLine()
    DocSel.Text = "* 参    数:"
    DocSel.NewLine()
    DocSel.Text = "* 返 回 值:"
    DocSel.NewLine()
    DocSel.Text = "* 作    者: Tanky Woo"
    DocSel.NewLine()
    DocSel.Text = "* 博    客: (1)www.WuTianQi.com | (2)www.TankyWoo.com"
    DocSel.NewLine()
    DocSel.Text = "* 电子邮箱:
admin@TankyWoo.com"
    DocSel.NewLine()
    DocSel.Text = "* 日    期:" + System.DateTime.Now.ToLongDateString()
    DocSel.NewLine()
    DocSel.Text = "*******************************************************************/"
End Sub
 
宏代码示例2:
Option Explicit Off
Option Strict Off

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports VSLangProj
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Windows.Forms


Public Module ModuleName

    'You can just define the variable
    Dim document As Document = DTE.ActiveDocument
    Dim selection As TextSelection = DTE.ActiveDocument.Selection
    Dim headerText As String
    Dim filename As String = document.Name
    Dim pathname As String = document.Path.ToString()

    Dim projectname As String = document.ProjectItem.ContainingProject.Name

    Public Sub AddFileHeader()
        Try

        'Must set value again because there is singleton module instance
            document = DTE.ActiveDocument
            selection = DTE.ActiveDocument.Selection

            filename = document.Name
            pathname = document.Path.ToString()
            projectname = document.ProjectItem.ContainingProject.Name

            selection.StartOfDocument()
            'deleteExistComment()
            insertComment()
        Finally
            Application.DoEvents()
        End Try
    End Sub

    Private Sub deleteExistComment()
        selection.StartOfDocument()
        DTE.ExecuteCommand("Edit.Find")
        DTE.Windows.Item(filename).Activate()
        DTE.Find.FindWhat = "using system"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Return
        End If
        DTE.Windows.Item(filename).Activate()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.LineUp(True, 100)
        DTE.ActiveDocument.Selection.Delete()
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
    End Sub

    Private Sub insertComment()
        'selection.StartOfDocument()

        selection.Insert("#region (C) Header Region @ " + Date.Today.Year.ToString())
        selection.NewLine()

        selection.Insert("//==============================================================")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("// The Herder Region@ " + Date.Today.Year.ToString())
        selection.NewLine()

        selection.Insert("// Copyright (C) 2010 - " + Date.Today.Year.ToString() + ". All rights reserved.")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("// File:")
        selection.NewLine()

        selection.Insert("//      " + filename)
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("// Description: Why do you create this file ")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("//==============================================================")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("// $History: " + document.FullName.Substring(document.FullName.IndexOf(":") + 1) + " $")
        selection.NewLine()

        selection.Insert("//")
        selection.NewLine()

        selection.Insert("// ****************** Version 1 ******************")
        selection.NewLine()

        selection.Insert("// User: Who      Time : " + Date.Now.ToLocalTime().ToString())
        selection.NewLine()

        selection.Insert("// Updated in: " + projectname + " Project ")
        selection.NewLine()

        selection.Insert("// Comments: What do you want to do ")
        selection.NewLine()

        selection.Insert("// ")
        selection.NewLine()

        selection.NewLine()

        selection.Insert("#endregion")
        selection.NewLine()

        selection.NewLine()
    End Sub
End Module

 
================================================================

以下的宏定义可以拿来作为学习宏操作之用

From :   http://tangxingqt.blog.163.com/blog/static/27710872200972815439359/
 
  
创建过程:

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) 科技 2009
'* 文件名称 : CMonitor
'* 当前版本 : 1.0.0.1
'* 作    者 :  (
tangxingqt@163.com)
'* 设计日期 : 2009年8月04日
'* 内容摘要 : VS2008 Comment Macro
'*******************************************************************************/
Public Module CMonitor
    Private Function Copyright()
        Copyright = CStr(Date.Today.Year) + "科技 All right reserved"
    End Function

    Private Function EMailAddress()
        EMailAddress = "
tangxingqt@163.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, ","))
                    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()
                    '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 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值