将Visual Studio中的光标位置转换为剪贴板

时间:2011-10-02 19:42:08

标签: visual-studio

通常在描述代码中的问题时,我需要通过行/列/函数来引用它。 是否有Visual Studio的宏/加载项为我复制该信息?

如果可以复制到剪贴板那将是完美的:文件,行,列,函数名称

但我会采取任何组合:)。

谢谢!

1 个答案:

答案 0 :(得分:0)

我最终做了一个宏。不幸的是我无法从宏访问剪贴板,所以我不得不使用NirCmd作为那部分。除此之外,它很棒!

Public Sub CopyLocation()

    Dim fileName = DTE.ActiveDocument.Name
    Dim line = ""
    Dim column = ""
    Dim functionName = ""
    Dim className = ""

    Dim textDocument = TryCast(DTE.ActiveDocument.Object, TextDocument)
    If textDocument IsNot Nothing Then

        Dim activePoint = textDocument.Selection.ActivePoint

        column = activePoint.DisplayColumn
        line = activePoint.Line

        Dim codeElement As CodeElement

        codeElement = activePoint.CodeElement(vsCMElement.vsCMElementFunction)
        If codeElement IsNot Nothing Then
            functionName = codeElement.Name
        End If

        codeElement = activePoint.CodeElement(vsCMElement.vsCMElementClass)
        If codeElement IsNot Nothing Then
            className = codeElement.Name
        End If

    End If

    Dim output As String = String.Format("File: {0} ", fileName)
    If (String.IsNullOrEmpty(line) = False) Then output = output & String.Format("Line: {0} ", line)
    If (String.IsNullOrEmpty(column) = False) Then output = output & String.Format("Column: {0} ", column)
    If (String.IsNullOrEmpty(className) = False) Then output = output & String.Format("at {0}", className)
    If (String.IsNullOrEmpty(functionName) = False) Then output = output & String.Format(".{0}", functionName)

    Dim process As System.Diagnostics.Process
    process.Start("c:\NoInstall files\nircmd.exe", String.Format("clipboard set ""{0}""", output))

End Sub