VS 自动添加注释
现在大多数公司都规定程序员在程序文件的头部加上版权信息,这样每个人写的文件都可以区分开来,如果某个文件出现问题就可以快速的找到文件的创建人,用最短的时间来解决问题,常常是以下格式:
//======================================================================
// 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中的类进行修改可以打开CSharp2052Class.zip.其中CSharp2052包括了所有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$,它就可以直接给我们加入创建的时间.我们可以对CSharp2052中所有的模板进行修改,切忌不要轻易修改系统那些代码,以免影响我们的正常的使用.
对于做Web开发的人员来说可以在ItemTemplatesCacheWebCSharp2052里进行修改.
新建一个类文件就可以实现了插入我们自定义的版权信息.
- //======================================================================
- //
- // 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: 在此处添加构造函数逻辑
- //
- }
- }
哈哈,是不是很酷呀!赶快个性一下你的代码吧!
这个是扩展参数
上面提供了部分的参数(希望有人继续补充),已经经本人在VS2010下测试,可以通过
模板
程序注释的重要性毋庸置疑,一个大型的项目通常情况下都是几个软件工程师合作出来的成果,假如该项目中的代码注释不全,那会给将来的维护者带来无穷无尽的隐患。
通用的办法是给自己工程里面的函数添加注释——使用宏。
1.打开Visual Studio 2008(2005一样适用)开发工具,单击“工具→宏→新建宏项目”,然后按照步骤建立注释宏,添加如下代码并保存。
2.打开 菜单 –> 工具–>选项 –> 键盘 ,在列表框中选择刚才添加的Macro,然后在 按快捷键中输入快捷键,点击”分配” 。
注释宏的代码如下:
- Sub AddFunComment()
- Dim DocSel As EnvDTE.TextSelection
- DocSel = DTE.ActiveDocument.Selection
- 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, )
- 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(":") + ) + " $")
- 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
- ================================================================
创建过程:
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
- '
- 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) - ))
- End Function
- Sub HeaderFileTemplate()
- If ActiveDocument.Language = EnvDTE.Constants.dsCPP Then ' C++
- If UCase(Right(ActiveDocument.Name, )) = ".H" Then '头文件
- Dim sGUID = GenGUIDString()
- Dim sFile = FileString(ActiveDocument.Name)
- Dim lens =
- Dim strDesc = "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- ActiveDocument.Selection.StartOfDocument()
- 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, ))
- Dim format2 = UCase(Right(ActiveDocument.Name, ))
- If format1 = ".C" Or format2 = ".CPP" Or format2 = ".CXX" Then '实现文件
- Dim Descr = "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf + _
- "" + vbLf
- ActiveDocument.Selection.StartOfDocument()
- ActiveDocument.Selection.text = Descr
- End If
- End If
- End Sub
- Dim ParamArr()
- Function StripTabs(ByVal MyStr)
- Do While InStr(MyStr, vbTab) <>
- 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, "(") -
- Dim Loc2 = InStr(Header, ")")
- If Loc > And Loc2 > 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, " ") <> Then
- retTp = retTp + Left(fcName, InStr(fcName, " "))
- fcName = Right(fcName, Len(fcName) - InStr(fcName, " "))
- End If
- 'Get the function parameters.
- Dim iPrm =
- Dim iPrmA =
- Dim prms = Header
- 'Count the number of parameters.
- Do While InStr(prms, ",") <>
- iPrm = iPrm +
- prms = Right(prms, Len(prms) - InStr(prms, ","))
- Loop
- 'Store the parameter list in the array.
- If iPrm > Then ' If multiple params.
- iPrm = iPrm +
- iPrmA = iPrm
- ReDim ParamArr(iPrm)
- Do While InStr(Header, ",") <>
- ParamArr(iPrm) = Left(Header, InStr(Header, ",") - )
- 'Remove brace from first parameter.
- If InStr(ParamArr(iPrm), " (") <> 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 -
- Loop
- ParamArr(iPrm) = Header
- 'Remove trailing brace from last parameter.
- If InStr(ParamArr(iPrm), ")") <> Then
- ParamArr(iPrm) = Left(ParamArr(iPrm), _
- InStr(ParamArr(iPrm), ")") - )
- Trim(ParamArr(iPrm))
- End If
- Else 'Possibly one param.
- ReDim ParamArr()
- Header = Right(Header, Len(Header) - ) ' Strip the first brace.
- Trim(Header)
- ParamArr() = StripTabs(Header)
- If InStr(ParamArr(), ")") <> Then
- ParamArr() = Left(ParamArr(), InStr(ParamArr(), ")") - )
- Trim(ParamArr())
- iPrmA =
- 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
- ActiveDocument.Selection.text = Descr
- End If
- End If
- End If
- End Sub
- End Module
参数 |
描述 |
clrversion |
当前系统CLR版本号 |
GUID [1-10] |
生成全局唯一标识符,可以生成10个 (例如:guid1) |
itemname |
打开添加新建项时输入的文件名称 |
machinename |
当前机器的名称(如:pc1) |
registeredorganization |
注册的组织名 |
rootnamespace |
命名空间名 |
safeitemname |
保存的文件名 |
time |
当前系统时间,格式:DD/MM/YYYY 00:00:00. |
userdomain |
用户所在的域 |
username |
当前系统用户名 |
year |
当前系统时间 YYYY |
参数 |
描述 |
clrversion |
当前系统CLR版本号 |
GUID [1-10] |
生成全局唯一标识符,可以生成10个 (例如:guid1) |
itemname |
打开添加新建项时输入的文件名称 |
machinename |
当前机器的名称(如:pc1) |
registeredorganization |
注册的组织名 |
rootnamespace |
命名空间名 |
safeitemname |
保存的文件名 |
time |
当前系统时间,格式:DD/MM/YYYY 00:00:00. |
userdomain |
用户所在的域 |
username |
当前系统用户名 |
year |
当前系统时间 YYYY |
VS 自动添加注释的更多相关文章
- PowerDesigner 如何添加每个表中共用的字段及自动添加注释
PowerDesigner 如何添加每个表中共用的字段: 有时候在创建表的时候会有一些共用的字段,但是每一张表都要去创建,这样做很麻烦,特别是这样重复的工作,稍不留意就会出现问题,实际上在PD中有这样 ...
- vs 文件头自动添加注释
原文:vs 文件头自动添加注释 vs2010 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates ...
- (转)Eclipse中自动添加注释(作者,时间)
方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code- ...
- Unity脚本自动添加注释脚本及排版格式
Unity脚本自动添加注释脚本及头部注释排版格式 公司开发项目,需要声明版权所有,,,,标注公司名,作者,时间,项目名称及描述等等. 自己总结实现的现成脚本及头部注释排版文本,添加到模版即可. 文件不 ...
- eclipse自动添加注释
自动添加注释 快捷键:alt shift jwindows-->preference Java-->Code Style-->Code Templates code-->new ...
- 【Eclipse】如何在Eclipse中如何自动添加注释和自定义注释风格
背景简介 丰富的注释和良好的代码规范,对于代码的阅读性和可维护性起着至关重要的作用.几乎每个公司对这的要求还是比较严格的,往往会形成自己的一套编码规范.但是再实施过程中,如果全靠手动完成,不仅效率低下 ...
- python基础===monkeytype可以自动添加注释的模块!
monkeytype 一个可以自动添加注释的模块! 先要下载: pip install monkeytype 以官网的sample code为例 #moudle.py def add(a, b): r ...
- 如何在Eclipse中如何自动添加注释和自定义注释风格
1. 如何自动添加注释 可通过如下三种方法自动添加注释: (1)输入“/**”并回车. (2)用快捷键 Alt+Shift+J(先选中某个方法.类名或变量名). (3)在右键菜单中选择“Source ...
- Idea_学习_05_Intellij Idea自动添加注释的方法
二.参考资料 1. Intellij Idea自动添加注释的方法
- [转]Intellij Idea自动添加注释的方法
Intellij Idea自动添加注释的方法 阿历Ali 关注 2017.08.20 21:22* 字数 914 阅读 2741评论 0喜欢 6 程序媛阿历终于要写第一篇关于开发的文章了!!! 阿历用 ...
随机推荐
- hrbust1841再就业(状态压缩dp)
本人刚学压缩dp,只能对这些水题写题解 一方面对自己的理解有加深作用 另一方面希望和各位大牛交流交流..... 如果有对状态dp不太了解的童鞋可以参考入门知识:http://wenku.baidu.c ...
- WinForm中MouseEnter和MouseLeave混乱的问题
MouseEnter+MouseLeave不行,我用了MouseMove+MouseLeave,效果一样 最近做个聊天的系统,仿照qq的界面设计,像qq聊天界面中字体.表情.截图等图片,鼠标放上去显示 ...
- appium 常用API
''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:79 ...
- git pull 然后 ahead of origin/master * commit 消失
本来显示 your branch is ahead origin/master * commit后来也许在master merge 这个分支后, 然后git pull, 就显示Your branch ...
- grep 使用或条件 ( grep -e )
test@k1rhel5822161:/home/test>cat 31 52 33 24567test@k1rhel5822161:/home/test>grep -e '2|3' 3t ...
- javascrpt事件
1.HTML事件处理程序:就是事件直接写在HTML文档中,其特点就是HTML和Js紧密的结合在一起,缺点就是修改不方便,需要改动js和HTML两处.比如: <button onclick=&qu ...
- Linux下怎么删除非空目录
rm -rf 目录名 解释: 1.r意思是删除目录,f意思是force的缩写,强制删除,不提示. 2.如果目录为空,可以用 "rmdir 目录名"删除. 3.如果目录不为空,可以用 ...
- PE文件学习系列三-PE头详解
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 最近比较忙 ...
- IE兼容问题,各类css hack代码(亲测有效)
现在大部分企业对浏览器兼容要求是IE7+或者IE8+,要求IE6的很少,此处一并写出. IE6: _margin-top: 20px; IE6+IE7: *margin-top: 20px; +mar ...
- CentOS 7 配置静态IP
1.查看MAC地址 2.修改/etc/sysconfig/network-scripts/ifcfg-[第一步中红框内的文字] 3.添加和修改内容如下: 4.修改/etc/resolv.conf 5. ...