C# Visual Studio-在不失去焦点的情况下移动光标
我的包中有一些工具窗口,当用户在工具窗口中执行某些操作时,我希望查看文档中的特定点 我尝试了以下代码:C# Visual Studio-在不失去焦点的情况下移动光标,c#,visual-studio,vs-extensibility,vspackage,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
// 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);
它实现了我想要的功能,但不幸的是,它将焦点放在了VisualStudio代码编辑器上,将其从我的工具窗口中移除。如果用户在“我的工具”窗口中键入内容,并且它突然将焦点移动到编辑器,则这是不好的
有没有其他方法可以在不失去焦点的情况下做到这一点?您是否尝试过将焦点移回原位?在代码之前存储ActiveWindow,在代码之后调用其Activate()方法。我不知道为什么我没有想到这一点!谢谢
// 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();