程序员如果更快的工作之 Visual Studio

       往往一个项目中有几个人或几十个人写的代码。而在代码维护的时候我们应该更明了的知道这个文件里的代码是什么作用、谁写的、什么时候写的等。

       就如下面的效果。

宏示例.jpg

       这里,我是通过Visual Studio的宏实现的,方法是网上找的(原来只支持C++,我新加了C#和VB的支持),

       宏代码如下。

 

Private Function Copyright()
        Copyright = "成都九遥 " + CStr(Date.Today.Year)
    End Function

    Private Function EMailAddress()
        EMailAddress = "matrixdom@126.com"
    End Function

    Private Function AuthorName()
        AuthorName = "齐.net"
    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 DTE.ActiveDocument.ProjectItem.Name.ToLower().EndsWith(".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 + _
                          "#ifndef __" + sFile + "_" + sGUID + "__" + vbLf + _
                          "#define __" + sFile + "_" + sGUID + "__" + vbLf + _
                          "" + vbLf + _
                          "/**********************************  头文件 ************************************/" + vbLf + _
                          "" + vbLf + _
                          "#ifdef  __cplusplus" + vbLf + _
                          "extern ""C"" " + "{" + vbLf + _
                          "#endif" + vbLf + _
                          "" + vbLf + _
                         "/********************************** 常量和宏 **********************************/" + vbLf + _
                          "" + vbLf + _
                          "/********************************** 数据类型 **********************************/" + vbLf + _
                          "" + vbLf + _
                          "/********************************** 函数声明 **********************************/" + vbLf + _
                          "" + vbLf + _
                          "/********************************** 类定义 ***********************************/" + vbLf + _
                          "" + vbLf + _
                          "#ifdef  __cplusplus" + vbLf + _
                          "}" + vbLf + _
                          "#endif" + vbLf + _
                          "#endif // end of __" + sFile + "_" + sGUID + "__" + vbLf

            ActiveDocument.Selection.StartOfDocument(0)
            ActiveDocument.Selection.text() = strDesc
        ElseIf DTE.ActiveDocument.ProjectItem.Name.ToLower().EndsWith(".cs") 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 + _
                          "********************************************************************************/ " & vbCrLf


            ActiveDocument.Selection.StartOfDocument(0)
            ActiveDocument.Selection.text() = strDesc
        ElseIf DTE.ActiveDocument.ProjectItem.Name.ToLower().EndsWith(".vb") 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 + _
                          "'*******************************************************************************/" + _
                           " "


            ActiveDocument.Selection.StartOfDocument(0)
            ActiveDocument.Selection.text() = strDesc
        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

 

 

下面是使用方法:

1.       下载附件中的宏文件

2.       在VS中按Alt+F8,打开宏资源管理器。

宏资源管理器.jpg

3.       在宏上面,点击右键,并选择"加载宏项目"加载刚才下载的宏项目文件

4.       现在,如果你已经打开了一个代码文档(.cs,.vb,.h)就可以在"宏资源管理器里单击"HeaderFileTemplate",这样就会自动加上我们的注释。

 

当然还可以更方便一点。

打开"工具"菜单,打开"选项"对话框。在"环境","键盘"里加一个映射方案。如下图。

键盘方案.jpg

这样,你就可以在文档里通过 Ctrl+数字键盘 * 来快速给文档加注释了。

 

附件:VS注释宏

posted @ 2009-10-20 09:19  齐.net  阅读(529)  评论(0编辑  收藏  举报