Visual Studio - 移动光标而不会失去焦点

时间:2011-05-07 09:31:47

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

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

我尝试过以下代码:

// 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代码编辑器上,将其从我的工具窗口中取出。如果用户在我的工具窗口中输入并且它突然将焦点移动到编辑器,这就不好了。

还有另一种方法可以做到这一点而不会失去焦点吗?

1 个答案:

答案 0 :(得分:2)

// 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();