【问题标题】:Get cursor location when running Macro in visual Studio在 Visual Studio 中运行宏时获取光标位置
【发布时间】:2011-03-12 14:45:19
【问题描述】:

我运行了一个宏,它将版权标题写入我的文档。目前在写header的时候,光标停留在header的末尾。

我想做的是捕获当前位置,写入标题,然后将光标返回到原始位置。

有谁知道如何做到这一点?

【问题讨论】:

    标签: visual-studio macros


    【解决方案1】:

    我想我明白了。

            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)
    

    这全部嵌套在我编写的宏中,用于自动为每个代码文件添加版权标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 2017-07-31
      相关资源
      最近更新 更多