2011-05-07 45 views
0

我的包中有一些工具窗口,当用户在工具窗口中执行某些操作时,我想将文档中的特定点放入视图中。Visual Studio - 移动光标而不会丢失焦点

我试过下面的代码:

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

有没有另一种方式来做到这一点而不失焦点?

+2

你试过把焦点移回去吗?在代码之前存储ActiveWindow,之后调用它的Activate()方法。 – 2011-05-07 12:13:14

+0

这个伎俩。我不知道为什么我没有想到这一点!谢谢。 – 2011-05-07 13:15:57

回答

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