作者:梁利锋 标签:DotNet

5258天前 (阅读:3763)
  首先,是一个作为输出主体的类,它使用在 OutputWindow 中新增 Pane 的方式创建输出窗口:

Imports EnvDTE
Imports System.Diagnostics

Public Class OuputPane

    Private OWp As OutputWindowPane

    Public Sub New(ByVal PaneName As String)
        Dim OW As OutputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
        For Each op As EnvDTE.OutputWindowPane In OW.OutputWindowPanes
            If op.Name = PaneName Then
                OWp = op
                Clear()
                Exit Sub
            End If
        Next
        OWp = OW.OutputWindowPanes.Add(PaneName)
    End Sub

    Public Sub Write(ByVal msg As String)
        OWp.OutputString(msg)
    End Sub

    Public Sub WriteLine(ByVal msg As String)
        OWp.OutputString(msg & vbCrLf)
    End Sub

    Public Sub Clear()
        OWp.Clear()
    End Sub
End Class


  然后,可以使用这个类作为主输出,我写了一个输出质数的方法:

Sub 输出100内的质数()
    Dim op As New OuputPane("质数")
    For i As Integer = 2 To 100
        If 是质数(i) Then
            op.WriteLine(i.ToString())
        End If
    Next
End Sub

Private Function 是质数(ByVal n As Integer) As Boolean
    For i As Integer = 2 To n - 1
        If (n Mod i) = 0 Then
            Return False
        End If
    Next
    Return True
End Function


  输出质数主要是为了在比较小型的 Hashtable 中能指定一个不容易出现 Hash 值重复的初始大小值,而我又无法记得这许多的指数。

  另外,我现在比较喜欢把 using、系统创建的代码加入 #region 中,以便被折叠起来,不要干扰编程的思路,不过,对于 #region 的编写也是颇觉罗嗦,所以,也写了一个函数来处理:

Sub AddRegion()
    Dim sel As TextSelection = DTE.ActiveDocument.Selection
    Dim s As String = IIf(sel.Text.Substring(0, 6) = "using ", "usings", "System Code")
    Dim tpl As Integer = sel.TopPoint.Line
    sel.MoveToPoint(sel.BottomPoint)
    sel.NewLine()
    sel.LineUp()
    sel.Text = String.Format("{0}#endregion", vbCrLf)
    sel.MoveToLineAndOffset(tpl, 1)
    sel.NewLine()
    sel.LineUp()
    sel.Text = String.Format("#region {1}{0}", vbCrLf, s)
    sel.LineUp()
    FindRegion()
    sel.WordRight()
    sel.EndOfLine(True)
End Sub

Private Sub FindRegion()
    Dim s As String = DTE.Find.FindWhat
    DTE.Find.FindWhat = "#region"
    DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
    DTE.Find.MatchCase = False
    DTE.Find.MatchWholeWord = True
    DTE.Find.Backwards = False
    DTE.Find.MatchInHiddenText = True
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    DTE.Find.Action = vsFindAction.vsFindActionFind
    DTE.Find.Execute()
    DTE.Find.FindWhat = s
End Sub


  这个函数会对被选择的语句行添加 region,而在执行中自动判断,如果是 using 语句组的话,加入的 region 标题是 usings,否则加入 System Code,而它结束后,会停留在添加的 region 标题上,等待用户修改。不过,这个函数没有处理 vb.net 的问题,如果需要的话,可以自行把关键字修改成 vb.net 的格式,应该就可以了。

That's the thniikng of a creative mind(非注册用户) 2015-1-19 19:32:21

That's the thniikng of a creative mind