【问题标题】:Visual Studio - Move cursor without losing focusVisual Studio - 移动光标而不失去焦点
【发布时间】:2011-05-07 09:31:46
【问题描述】:

我的包中有一些工具窗口,当用户在工具窗口中执行某些操作时,我想将文档中的特定点显示在视图中。

我尝试了以下代码:

// Perform selection
TextSelection selection = activeDocument.Selection as TextSelection;
selection.MoveToAbsoluteOffset(offset, false);

// Show the currently selected line at the top of the editor if possible
TextPoint tp = (TextPoint)selection.TopPoint;
tp.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

它做我想做的事,但不幸的是,它把焦点放在了 Visual Studio 代码编辑器上,把它从我的工具窗口中移走了。如果用户在我的工具窗口中键入内容并且它突然将焦点移到编辑器上,这并不好。

有没有其他方法可以做到这一点而不会失去焦点?

【问题讨论】:

  • 您是否尝试将焦点移回?在代码之前存储ActiveWindow,之后调用它的Activate()方法。
  • 这成功了。我不知道为什么我没有想到!谢谢。

标签: c# visual-studio vs-extensibility vspackage


【解决方案1】:
// Store active window before selecting
Window activeWindow = applicationObject.ActiveWindow;

// Perform selection
TextSelection selection = activeDocument.Selection as TextSelection;
selection.MoveToAbsoluteOffset(offset, false);

// Show the currently selected line at the top of the editor if possible
TextPoint tp = (TextPoint)selection.TopPoint;
tp.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

// Restore focus to active window
activeWindow.Activate();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多