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

 

 

 

函数宏的优缺点

         老的C语言程序员中有一种倾向,就是把很短的执行频繁的计算写成宏,而不是定义为函数。完成I / O的g e t c h a r,做字符测试的i s d i g i t都是得到官方认可的...
  • linux510zcf
  • linux510zcf
  • 2009-11-09 11:25:00
  • 4781

用宏初始化结构体

#define INIT_STRUCT_PID {                        \     .count         = ATOMIC_INIT(1),            ...
  • SunnyBeiKe
  • SunnyBeiKe
  • 2011-10-12 21:18:04
  • 1094

C语言宏的定义及使用

一般在C程序中都会看到宏的定义与使用define,一般会有下面几种用法, 1、在C或C++语言源程序中允许用一个标识符来表示一个字符串,称为“宏”。   #define M (a+b)  ...
  • hhlcc520
  • hhlcc520
  • 2016-04-01 10:11:54
  • 129

C/C++ 中的assert()宏 断言机制

ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE (0), 程序将报告错误,并终止执行。如果表达式不为0,则继续执行后面的语句。这个宏通常原来判断...
  • u013467442
  • u013467442
  • 2014-07-14 14:39:37
  • 1187

用宏对结构体对象进行初始化

// StructInitial.h   #ifndef STRUCTINITIAL_H #define STRUCTINITIAL_H   typedef struct tStudent ...
  • huozhezhenhao
  • huozhezhenhao
  • 2011-08-16 10:47:33
  • 402

用宏实现动态数组

    提示:以下代码均未经详细测试。如果哪位有空,提出点意见,不胜感激,另本主题是用宏实现,所以不相关的就不要多说了。 #ifndef _VECTOR_H_#define _VECTOR_H_ #i...
  • zky
  • zky
  • 2004-07-21 20:58:00
  • 1134

iOS一些提高开发效率的宏(收集)

开发过程中使用一些宏可以大大提高开发效率,部分收集来自于http://blog.csdn.net/duxinfeng2010/article/details/9067947// // defineM...
  • zww1984774346
  • zww1984774346
  • 2016-07-05 18:04:53
  • 469

9.2 输入两个整数,求它们相除的余数。用带参的宏来实现,编程序。

9.2 输入两个整数,求它们相除的余数。用带参的宏来实现,编程序。 #include//9.2 输入两个整数,求它们相除的余数。用带参的宏来实现,编程序。 #define Y(a,b) a%b i...
  • qslife
  • qslife
  • 2016-08-22 21:13:00
  • 2673

C标准预定义宏,调试时加打印很有用

#include int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE__); printf("Fu...
  • Jeffrey0000
  • Jeffrey0000
  • 2015-07-22 20:11:37
  • 838
收藏助手
不良信息举报
您举报文章:vs2003用宏来增加效率
举报原因:
原因补充:

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