3:'Copyright(c)2011YONG.Allrightsreserved.4:'5:'

Visual Studio 2010利用宏添加注释

系统 1802 0

Visual Studio 2010 (下面简称 VS )编写类的过程中通常会在类的前面写上如下注释:

        
             1:  
        
        
          '------------------------------------------------------------------------------
        
      
        
             2:  
        
        
          ' <copyright file="***.vb" company="YONG">
        
      
        
             3:  
        
        
          '     Copyright (c) 2011 YONG. All rights reserved.
        
      
        
             4:  
        
        
          ' <copyright>
        
      
        
             5:  
        
        
          ' <author>郗晓勇<author>
        
      
        
             6:  
        
        
          ' <author>博客地址http://blog.csdn.net/beijiguangyong<author>
        
      
        
             7:  
        
        
          ' <date>2011年4月29日<date>
        
      
        
             8:  
        
        
          ' <description>
        
      
        
             9:  
        
        
          '
        
      
        
            10:  
        
        
          '
        
      
        
            11:  
        
        
          '
        
      
        
            12:  
        
        
          '
        
      
        
            13:  
        
        
          ' <description>
        
      
        
            14:  
        
        
          '------------------------------------------------------------------------------
        
      

每次复制粘贴未免太麻烦,我们可以利用 VS 的宏来为我们自动添加如上注释。具体操作如下:

1. 找到 VS IDE 宏编辑器

image

2. 打开后选择添加 Model

image

3. 起一个自己喜欢的名字(这里以HeadFileNote为例)

image

4. 点击添加后出现如下编辑界面

image <style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

5. 在右侧即可编辑自己所需要的“宏”了,我们将添加的“宏”代码如下

        
             1:  
        
        
          Imports
        
         System
      
        
             2:  
        
        
          Imports
        
         EnvDTE
      
        
             3:  
        
        
          Imports
        
         EnvDTE80
      
        
             4:  
        
        
          Imports
        
         EnvDTE90
      
        
             5:  
        
        
          Imports
        
         EnvDTE90a
      
        
             6:  
        
        
          Imports
        
         EnvDTE100
      
        
             7:  
        
        
          Imports
        
         System.Diagnostics
      
        
             8:  
        
      
        
             9:  
        
        
          Public
        
        
          Module
        
         HeadFileNote
      
        
            10:  
        
        
          Sub
        
         DocumentFileHeader()
      
        
            11:  
        
        
          Dim
        
         doc 
        
          As
        
         Document
      
        
            12:  
        
        
          Dim
        
         docName 
        
          As
        
        
          String
        
      
        
            13:  
        
        
          Dim
        
         companyName 
        
          As
        
        
          String
        
         = 
        
          "YONG"
        
      
        
            14:  
        
        
          Dim
        
         authorName 
        
          As
        
        
          String
        
         = 
        
          "郗晓勇"
        
      
        
            15:  
        
        
          Dim
        
         authorContact 
        
          As
        
        
          String
        
         = 
        
          "我的博客地址http://blog.csdn.net/beijiguangyong"
        
      
        
            16:  
        
        
          Dim
        
         copyrightText 
        
          As
        
        
          String
        
         = 
        
          String
        
        .Format(
        
          "Copyright (c) {0} {1}. All rights reserved."
        
        , 
        
          Date
        
        .Now.Year, companyName)
      
        
            17:  
        
      
        
            18:  
        
        
          ' 从程序中获得文件的名字
        
      
        
            19:  
        
                doc = DTE.ActiveDocument
      
        
            20:  
        
      
        
            21:  
        
        
          '获得当前编辑类的名字
        
      
        
            22:  
        
                docName = doc.Name
      
        
            23:  
        
      
        
            24:  
        
        
          ' 将添加焦点定位在文件首部
        
      
        
            25:  
        
                DTE.ActiveDocument.Selection.StartOfDocument()
      
        
            26:  
        
      
        
            27:  
        
        
          ' 添加一个版权说明
        
      
        
            28:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'------------------------------------------------------------------------------"
        
        
          '以String类型添加自己想要的符号、文字
        
      
        
            29:  
        
                DTE.ActiveDocument.Selection.NewLine() 
        
          '添加一个空行
        
      
        
            30:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <copyright file="
        
        
          ""
        
         + docName + 
        
          ""
        
        
          " company="
        
        
          ""
        
         + companyName + 
        
          ""
        
        
          ">"
        
      
        
            31:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            32:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'     "
        
         + copyrightText
      
        
            33:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            34:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <copyright>"
        
      
        
            35:  
        
      
        
            36:  
        
        
          ' 添加用户相关信息
        
      
        
            37:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            38:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <author>"
        
         + authorName + 
        
          "<author>"
        
      
        
            39:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            40:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <author>"
        
         + authorContact + 
        
          "</author>"
        
      
        
            41:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            42:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <date>"
        
         + 
        
          String
        
        .Format(
        
          "{0:D}"
        
        , 
        
          Date
        
        .Now) + 
        
          "<date>"
        
      
        
            43:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            44:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <description>"
        
      
        
            45:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            46:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'"
        
      
        
            47:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            48:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'"
        
      
        
            49:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            50:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'"
        
      
        
            51:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            52:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'"
        
      
        
            53:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            54:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "' <description>"
        
      
        
            55:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            56:  
        
                DTE.ActiveDocument.Selection.Text = 
        
          "'------------------------------------------------------------------------------"
        
      
        
            57:  
        
                DTE.ActiveDocument.Selection.NewLine()
      
        
            58:  
        
        
          End
        
        
          Sub
        
      
        
            59:  
        
        
          End
        
        
          Module
        
      

6. 添加完毕后点击保存

image

这样我们的“宏”就编好了,下面接着介绍怎样定义快捷键为我们的类添加头注释。

7. 进入我们的 VS 开发环境打开选项,进行相关设置

image

8. 设置完成后单击“ Assign ”(注册快捷键)如图

image

9. 单击“ OK ”完成所有设置,现在就去按下快捷键试试自己编辑的“注释添加宏”吧。

image <style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

Visual Studio 2010利用宏添加注释


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论