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++宏,普通函数,内联函数的运行速度以及三者的差异

下面论证一下:c/c++中 运行速度对比,宏>内联函数>普通函数 首先简单的代码验证下: #include "stdafx.h" #include #include #define ...
  • arxobject
  • arxobject
  • 2016年08月11日 23:46
  • 641

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

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

vs中的一些编译宏定义

1._DEBUG  用来指示 当前编译是rlease  还是debug  ,可用这个宏定义 来区分d ,r库文件的链接 2.判断是32位编译还是64位编译。 在 Win32 配置下,_WIN3...
  • hero_heart
  • hero_heart
  • 2016年02月25日 11:23
  • 1993

视频聊天vs2003工程转vs2010工程问题记录

今天需要将一份vs2003上面的工程转成vs2010下面的,在转换过程中出现了如下问题,特作此记录。 1.头文件streams.h与qedit.h找不见 streams.h是directshow工程中...
  • fengshuiyue
  • fengshuiyue
  • 2013年12月02日 13:35
  • 1736

VS中的路径宏

说明 $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值。有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置。 $(References) ...
  • happyever2012
  • happyever2012
  • 2015年03月02日 13:37
  • 444

for循环用效率分析(++和--时效率的差异分析)

(听别人说的面试中问过的一个问题,记不清了,如有偏差希望大家补充纠正) 问题:完成相同的功能,for循环使用++和–哪个效率更高? 通常我们在编写需要for循环实现的程序时有两种实现方法: fo...
  • hyqsong
  • hyqsong
  • 2015年11月10日 07:56
  • 1457

MySQL提升效率之limit

分页查询在我们的实际工作中经常会遇到,但是对于不同的数据库,分页查询的实现方式也不一样。而在MySQL中的分页就是靠limit实现的,可是limit的效率怎么样呢,我们来看看。 我们在写代码的...
  • u010168160
  • u010168160
  • 2016年09月26日 11:42
  • 556

Linux学习笔记——例说makefile 增加宏定义

从学习C语言开始就慢慢开始接触makefile,查阅了很多的makefile的资料但总感觉没有真正掌握makefile,如果自己动手写一个makefile总觉得非常吃力。所以特意借助博客总结makef...
  • maopig
  • maopig
  • 2017年04月06日 14:27
  • 2329

VS中的路径宏

$(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值。有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置。 $(References) 以分号分隔的引用列表被...
  • whatday
  • whatday
  • 2014年12月29日 16:39
  • 5514

makefile 增加宏定义

http://blog.csdn.net/xukai871105/article/details/37079159 0.前言     从学习C语言开始就慢慢开始接触makefile,查...
  • freedom8531
  • freedom8531
  • 2015年01月29日 14:10
  • 307
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vs2003用宏来增加效率
举报原因:
原因补充:

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