拷贝代码以及其行号的脚本

有的时候你需要打印源代码和源代码的行号,或者将源代码及其行号拷贝到其他文档中,例如将源代码拷贝到Word,你希望得到类似下面的结果:

1. using System;

2. using System.Collections.Generic;

3. using System.Configuration;

4. using System.Data;

5. using System.Linq;

6. using System.Windows;

7.

8. namespace Test

9. {

10.     /// <summary>

11.     /// Interaction logic for App.xaml

12.     /// </summary>

13.     public partial class App : Application

14.     {

15.     }

16. }

下面是一个VB宏,用来完成上面我所说的工作:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports System.Diagnostics

 

Public Module Module1

    Sub CopyNumberedLines()

        Dim textSelection As EnvDTE.TextSelection

        Dim textSelectionNewFile As EnvDTE.TextSelection

        Dim i As Integer

        Dim lineCount As Integer

        Dim newText As String

 

        textSelection = DTE.ActiveDocument.Selection

        textSelection.SelectAll()

        textSelection.Copy()

        textSelectionNewFile = DTE.ItemOperations.NewFile("General/Visual C# Class").Selection

        textSelectionNewFile.SelectAll()

        textSelectionNewFile.Paste()

        textSelectionNewFile.StartOfDocument()

 

        textSelectionNewFile.SelectAll()

        lineCount = textSelectionNewFile.BottomLine

        textSelectionNewFile.StartOfDocument()

 

        For i = 1 To (lineCount)

            textSelectionNewFile = DTE.ActiveDocument.Selection

            textSelectionNewFile.StartOfLine(EnvDTE.vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

            newText = CStr(i) & ". "

            textSelectionNewFile.Text = newText

            textSelectionNewFile.LineDown(True)

        Next

        textSelection = DTE.ActiveDocument.Selection

        textSelection.SelectAll()

        textSelection.Copy()

 

        DTE.ActiveDocument.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo)

    End Sub

End Module

在运行的时候,只要打开源代码文件,点击VS菜单里面的“工具”--> “宏”--> “宏资源管理器”,找到你的宏(以函数名命名),右键单击执行就可以了,如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值