Visual Studio - Move cursor without losing focus
I have some tool windows in my package and I would like to bring a specific point in the document into view when the user performs some actions in the tool windows.
I've tried the following code:
// 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);
It does what I want but unfortunately gives focus to the Visual Studio code editor, taking it away from my tool window. This is not good if the user is typing in my tool window and it suddenly 开发者_如何学运维moves focus to the editor.
Is there another way to do this without losing focus?
// 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();
精彩评论