vs2003用宏来增加效率

原创 2005年03月03日 15:24:00

代码敲多了,难免会有很多重复的工作,如为每个成员函数和参数写说明文档等,为此可以宏来提高效率.下面是我写的和一些改进后的宏,可以添加到vs2003的宏管理器里面,就可以使用了.

 

Option Strict Off

Option Explicit Off

 

Imports EnvDTE

Imports System.Diagnostics

Imports System.Windows

Imports System.Windows.Forms

Imports System

Imports System.Collections.Specialized

 

Public Module OwnMacros

 

    '在选择的文本前面粘贴文本

    Public Sub PasteAtBegin()

        PasteText(0)

    End Sub

 

    '在选择的文本最后粘贴文本

    Public Sub PasteAtEnd()

        PasteText(1)

    End Sub

 

    '在选择的文本前后粘贴文本

    Public Sub PasteAtBeginEnd()

        PasteText(2)

    End Sub

 

 

 

    '粘贴文本

    Private Sub PasteText(ByVal position As Integer)

        Dim beginline, endline As Integer

        Dim objTextSelection As TextSelection

        Dim i As Integer

        objTextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)

        beginline = objTextSelection.AnchorPoint.Line

        endline = objTextSelection.BottomPoint.Line

        For i = beginline To endline

            objTextSelection.GotoLine(i, False)

            If position = 0 Or position = 2 Then

                objTextSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

                objTextSelection.Paste()

            End If

 

            If position = 1 Or position = 2 Then

                objTextSelection.EndOfLine()

                objTextSelection.Paste()

            End If

 

        Next

    End Sub

 

对多行文本进行粘贴

    Sub MultiLinePaste()

        Dim beginrow, endrow As Integer

        Dim objTextSelection As TextSelection

        Dim i As Integer

        Dim value As New StringCollection

 

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

 

        beginrow = objTextSelection.AnchorPoint.Line

        endrow = objTextSelection.BottomPoint.Line

 

        If beginrow < endrow Then

            For i = beginrow To endrow

                objTextSelection.GotoLine(i, False)

                objTextSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

                objTextSelection.EndOfLine(True)

                value.Add(objTextSelection.Text)

                objTextSelection.Delete()

            Next

 

            objTextSelection.GotoLine(beginrow, False)

            objTextSelection.Paste()

 

            For i = beginrow To endrow

                objTextSelection.GotoLine(i, False)

                objTextSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

                objTextSelection.Insert(value.Item(i - beginrow))

                objTextSelection.Insert("  ")

            Next

            value.Clear()

 

        End If

 

    End Sub

 

    Sub MultiLineRepert()

        Dim beginrow, endrow As Integer

        Dim objTextSelection As TextSelection

        Dim i As Integer

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

        beginrow = objTextSelection.AnchorPoint.Line

        endrow = objTextSelection.BottomPoint.Line

        If beginrow < endrow Then

            For i = beginrow To endrow

                objTextSelection.GotoLine(i, False)

                objTextSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

                objTextSelection.EndOfLine(True)

                objTextSelection.Copy()

                objTextSelection.EndOfLine(False)

                objTextSelection.Insert(Microsoft.VisualBasic.Constants.vbTab + Microsoft.VisualBasic.Constants.vbTab)

                objTextSelection.Paste()

            Next

        End If

    End Sub

 

 

    '对于选中的文本添加vb注释

    Public Sub AddVbComment()

        AddCommentInfo(0)

    End Sub

 

    '对于选中的文本添加vc注释

    Public Sub AddVcComment()

        AddCommentInfo(1)

    End Sub

 

    '添加注释文本

    Private Sub AddCommentInfo(ByVal type As Integer)

        Dim objTextSelection As TextSelection

        Dim comment As String

        Dim text As String

 

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

        text = objTextSelection.Text

 

        If type = 0 Then

            comment = "'"

        End If

 

        If type = 1 Then

            comment = "//"

        End If

 

 

        objTextSelection.LineUp()

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "<摘要>"

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + text

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "</摘要>"

 

    End Sub

 

    Public Sub ClearScript()

        Dim beginPoint As VirtualPoint

        Dim endPoint As VirtualPoint

        Dim objTextSelection As TextSelection

 

        Dim b, e As Integer

        Dim bline, eline As Integer

 

        Dim findresult As Boolean

 

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

 

        'objTextSelection.StartOfDocument(False)

 

 

        findresult = True

 

 

        While findresult = True

 

            findresult = False

 

            If objTextSelection.FindText("<script", vsFindOptions.vsFindOptionsMatchWholeWord) Then

                beginPoint = objTextSelection.AnchorPoint

                bline = beginPoint.Line

                b = beginPoint.AbsoluteCharOffset

            Else

                Exit While

            End If

 

            If objTextSelection.FindText("/script>", vsFindOptions.vsFindOptionsMatchWholeWord) Then

                endPoint = objTextSelection.BottomPoint

                eline = endPoint.Line

                e = endPoint.AbsoluteCharOffset

                findresult = True

            End If

 

            If (bline <= eline) And ((eline - bline) <= 3) Then

                objTextSelection.MoveToAbsoluteOffset(b)

                objTextSelection.MoveToAbsoluteOffset(e, True)

                objTextSelection.Delete()

            Else

                'objTextSelection.WordRight(False)

                objTextSelection.MoveToPoint(objTextSelection.BottomPoint)

            End If

        End While

 

        'DTE.ActiveDocument.Save()

        DTE.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes)

 

    End Sub

 

     添加最后修改信息

    Public Sub AddLastModify()

        Dim objTextSelection As TextSelection

        Dim comment As String

        comment = LineOrientedCommentStart()

 

        If comment = "" Then

            comment = "--"

        End If

 

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

 

        objTextSelection.StartOfDocument(False)

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "#################################################"

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "修改: "

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "修改时间: "

        objTextSelection.Text += System.DateTime.Now.ToLongDateString() + " " + System.DateTime.Now.ToLongTimeString()

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "#################################################"

        objTextSelection.NewLine()

        objTextSelection.NewLine()

 

 

    End Sub

 

     添加代码版本信息

    Public Sub AddVersionInfo()

        Dim objTextSelection As TextSelection

        Dim comment As String

        comment = LineOrientedCommentStart()

 

        If comment = "" Then

            comment = "--"

        End If

 

        objTextSelection = CType(DTE.ActiveDocument.Selection, EnvDTE.TextSelection)

 

        objTextSelection.StartOfDocument(False)

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "#################################################"

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment

 

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "版本: "

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "作者: "

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "说明: "

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "最后修改: "

        objTextSelection.Text += System.DateTime.Now.ToLongDateString() + " " + System.DateTime.Now.ToLongTimeString()

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment

 

        objTextSelection.NewLine()

        objTextSelection.Text = comment + "#################################################"

 

        objTextSelection.NewLine()

 

        objTextSelection.NewLine()

 

        objTextSelection.NewLine()

 

    End Sub

 

    Private Sub CommentCodeType(ByRef celttype As CodeType)

        Dim celt As CodeElement

 

        Dim ep As EditPoint = celttype.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()

        Dim commentStart As String = LineOrientedCommentStart()

        If (commentStart.Length = 2) Then

            commentStart = commentStart & commentStart.Chars(1) & " "

        ElseIf (commentStart.Length = 1) Then

            commentStart = commentStart & commentStart.Chars(0) & commentStart.Chars(0) & " "

        End If

 

        Try

            DTE.UndoContext.Open("Insert Doc Comments")

            For Each celt In celttype.Members

 

                '通过检验上一行的文本来决定该成员是否已经注释过了

                ep.MoveToPoint(celt.GetStartPoint(vsCMPart.vsCMPartHeader))

                ep.LineUp()

                Dim alreadycheck As String = Trim(ep.GetLines(ep.Line, ep.Line + 1))

                If (alreadycheck = Trim(commentStart)) Then

                    GoTo NEXT_LOOP

                End If

 

                If (celt.Kind = vsCMElement.vsCMElementFunction) Then

                    Dim codefun As CodeFunction = celt

                    ep.MoveToPoint(codefun.GetStartPoint(vsCMPart.vsCMPartHeader))

                    Dim params As CodeElements = codefun.Parameters

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<摘要>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "摘要: " & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</摘要>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

 

                    Dim celt2 As CodeElement

                    Dim cp As CodeParameter

                    For Each celt2 In params

                        cp = celt2

                        ep.Insert("<param name=" & cp.Name & " desc=></param>")

                        ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                        ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

                    Next 'param

                End If 'we have a function

 

                If (celt.Kind = vsCMElement.vsCMElementProperty) Then

                    Dim codeprop As CodeProperty = celt

                    ep.MoveToPoint(codeprop.GetStartPoint(vsCMPart.vsCMPartHeader))

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<属性>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "说明:" & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</属性>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

                End If

 

 

 

                If (celt.Kind = vsCMElement.vsCMElementVariable) Then

                    Dim code As CodeVariable = celt

                    ep.MoveToPoint(code.GetStartPoint(vsCMPart.vsCMPartHeader))

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<成员>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "说明:" & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</成员>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

                End If

 

                If (celt.Kind = vsCMElement.vsCMElementStruct) Then

 

                    Dim code As CodeStruct = celt

                    ep.MoveToPoint(code.GetStartPoint(vsCMPart.vsCMPartHeader))

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<结构>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "说明:" & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</结构>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

 

                End If

 

NEXT_LOOP:

 

 

            Next 'code elt member

        Finally

            DTE.UndoContext.Close()

        End Try

 

 

    End Sub

 

    '为vb代码添加中文注释

    Sub InsertChsComments()

        Dim projitem As ProjectItem = DTE.ActiveDocument.ProjectItem

        Dim filecm As FileCodeModel = projitem.FileCodeModel

        Dim celt As CodeElement

        Dim celttype As CodeType

        Dim i As Integer

        Dim j As Integer

 

        For i = 1 To filecm.CodeElements.Count

            celt = filecm.CodeElements.Item(i)

 

            If (TypeOf celt Is CodeNamespace) Then

 

                Dim tt As CodeNamespace

                tt = celt

 

                For j = 1 To tt.Members.Count

 

                    'celt = celt.members.item(1)

                    celt = tt.Members.Item(j)

 

                    If (TypeOf celt Is CodeType) Then

                        celttype = CType(celt, CodeType)

                    Else

                        Throw New Exception("Didn't find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")

                    End If

                    CommentCodeType(celttype)

                Next

            Else

                If (TypeOf celt Is CodeType) Then

                    celttype = CType(celt, CodeType)

                Else

                    Throw New Exception("Didn't find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")

                End If

                CommentCodeType(celttype)

            End If

 

 

 

 

 

        Next

 

    End Sub

 

 

    Private Sub InsertDocChsComments()

        Dim projitem As ProjectItem = DTE.ActiveDocument.ProjectItem

        Dim filecm As FileCodeModel = projitem.FileCodeModel

        Dim celt As CodeElement = filecm.CodeElements.Item(1)

        Dim celttype As CodeType

 

        If (TypeOf celt Is CodeNamespace) Then

            celt = celt.members.item(1)

        End If

        If (TypeOf celt Is CodeType) Then

            celttype = CType(celt, CodeType)

        Else

            Throw New Exception("Didn't find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")

        End If

 

 

        Dim ep As EditPoint = celttype.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()

 

        Dim commentStart As String = LineOrientedCommentStart()

        If (commentStart.Length = 2) Then

            commentStart = commentStart & commentStart.Chars(1) & " "

        ElseIf (commentStart.Length = 1) Then

            commentStart = commentStart & commentStart.Chars(0) & commentStart.Chars(0) & " "

        End If

 

        Try

            DTE.UndoContext.Open("Insert Doc Comments")

            For Each celt In celttype.Members

                If (celt.Kind = vsCMElement.vsCMElementFunction) Then

                    Dim codefun As CodeFunction = celt

                    ep.MoveToPoint(codefun.GetStartPoint(vsCMPart.vsCMPartHeader))

                    Dim params As CodeElements = codefun.Parameters

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<摘要>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "摘要: " & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</摘要>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

 

                    Dim celt2 As CodeElement

                    Dim cp As CodeParameter

                    For Each celt2 In params

                        cp = celt2

                        ep.Insert("<param name=" & cp.Name & "></param>")

                        ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                        ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

                    Next 'param

                End If 'we have a function

 

                If (celt.Kind = vsCMElement.vsCMElementProperty) Then

                    Dim codeprop As CodeProperty = celt

                    ep.MoveToPoint(codeprop.GetStartPoint(vsCMPart.vsCMPartHeader))

 

                    'Dim params As CodeElement = codeprop.Attributes

 

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.LineUp()

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<属性>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "说明:" & celt.Name & ".")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</属性>")

                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

 

                    'Dim celt2 As CodeElement

                    'Dim cp As CodeParameter

                    'For Each celt2 In params

                    '    cp = celt2

                    '    ep.Insert("< param name=" & cp.Name & "></param>")

                    '    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)

                    '    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)

                    'Next

 

                End If

 

            Next 'code elt member

        Finally

            DTE.UndoContext.Close()

        End Try

    End Sub

 

    '返回注释类型

    Function LineOrientedCommentStart(Optional ByVal doc As Document = Nothing) As String

        If (doc Is Nothing) Then

            doc = DTE.ActiveDocument

        End If

 

        Dim ext As String = doc.Name

        If (ext.EndsWith(".cs")) Then

            Return "//"

        ElseIf (ext.EndsWith(".cpp")) Then

            Return "//"

        ElseIf (ext.EndsWith(".h")) Then

            Return "//"

        ElseIf (ext.EndsWith(".vb")) Then

            Return "'"

        ElseIf (ext.EndsWith(".idl")) Then

            Return "//"

        End If

    End Function

 

End Module

 

 

 

相关文章推荐

vc6.0 和vs2003注释宏

  • 2010年05月13日 14:18
  • 73KB
  • 下载

完美解决 VS2003/2005 代码编辑器字体问题的方案

在很长的一段时间内,Visual Studio 2003/2005的代码编辑器中的编辑字体设置,对中国的程序员来说,是一个挠头的问题。许多人在代码编辑器中使用的无非是宋体、新宋体,有的安装了Offic...
  • leotnt
  • leotnt
  • 2011年08月26日 23:48
  • 799

VS.NET 2003 + MySQL Server 5.1 编写第一个MySQL控制台程序 (一)

首先,我们必须下载 MySQL Server 5.1 。是那种完整安装包,安装后在 MySQL Server 的安装目录里要有 include 和 lib\debug 文件夹,其中必须包含开发 MyS...
  • gmbros
  • gmbros
  • 2017年02月23日 16:47
  • 363

VS2003 试图运行项目时出错: 无法在 Web 服务器上启动调试。您不具备调试此应用程序的权限。

今天因为项目需要,安装了.NET2003,但BS项目报错“试图运行项目时出错: 无法在 Web 服务器上启动调试。您不具备调试此应用程序的权限。此项目的 URL 位于 Internet 区域。有关更多...

安装和配置VS.NET 2003和SQL SERVER 2005

1、首先安装IIS。 工具/原料 环境:windows xp 步骤/方法 1 先在百度找到IIS 6.0,然后下载下来,当然其它的版本也可以,我这个是为了凑合系统。  ...

基于MATLAB2008B与VS 2003生成独立可执行的程序

如何将MATLAB程序编译成独立可执行的程序?如何将编译好的独立可执行程序发布在没有安装MATLAB的电脑上?下面将一步步实现: 一、生成独立可执行的程序(exe文件)步骤 1、安装编译器...
  • xhhjin
  • xhhjin
  • 2011年11月10日 14:28
  • 839

VS2003监视窗口(watch)的使用

VS2003监视窗口的使用1.        可以对当前变量赋值。 图1现在直接在监视窗口把j的值改为1: 图2可以使用这种方式来对特定的条件值进行调试。2.        也可以直接填写条件表达式:...

VS2003.net ActiveX控件相关

今天在VC7上开发ActiveX控件,添加函数害的我挣了一下午,现在想想真是怀恋VC6的ClassWizard啊。在网上找了很多资料,也没发现与此相关的,所以就记录下来,仅供参考啊 创建Acti...

win7 vs.net2003 调试webservice 无法单步执行

现有项目是用vs.net2003开发,客户端是winform,服务端是webservice,两个项目在一个解决方案中。原来用的是win2003,可以在解决方案中单步执行程序调试,遇到webservic...

vs2003+快捷键+大本营

Ctrl+Shift +Esc   任务管理器 vs2003+快捷键+大本营 Posted on 2005-09-27 15:43 逍遥游 阅读(289) 评论(0) ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vs2003用宏来增加效率
举报原因:
原因补充:

(最多只允许输入30个字)