2011-05-07 44 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();