在Visual Studio中运行宏时获取光标位置

时间:2010-07-07 02:28:00

标签: visual-studio macros

我有一个我运行的宏,它会在我的文档中写入版权标题。目前,当写入标题时,光标将保留在标题的末尾。

我希望能够捕获当前位置,编写标题,然后将光标返回原始位置。

有谁知道如何实现这一目标?

1 个答案:

答案 0 :(得分:7)

我想我已经得到了它。

        Dim selection As TextSelection = DTE.ActiveDocument.Selection
        ''# store the original selection and cursor position
        Dim topPoint As TextPoint = selection.TopPoint
        Dim bottomPoint As TextPoint = selection.BottomPoint
        Dim lTopLine As Long = topPoint.Line
        Dim lTopColumn As Long = topPoint.LineCharOffset
        Dim lBottomLine As Long = bottomPoint.Line
        Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
        Dim verticalOffset As Integer = 0

        ''# do a bunch of stuff that adds text to the page

        ''# Restore cursor to previous position
        selection.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
        selection.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)

这一切都嵌套在我编写的宏中,以自动为每个代码文件添加版权标题。