![](/img/trans.png)
[英]How to display custom tool window at the first position, programmatically in visual studio
[英]How to retrieve a tool window's position in Visual Studio
在我正在開發的 Visual Studio 擴展中,我提供了一個自定義工具窗口。
我試圖找出用戶是否將工具窗口停靠在一側(左/右/上/下)或者它是否處於浮動狀態。 有沒有辦法計算我的工具窗口的當前位置,並且在用戶完成移動它之后是否有要知道的事件?
一些背景知識:我正在為 Visual Studio 開發 CodeStream 擴展。 當用戶選擇代碼時,我們會在 CodeStream 工具窗口(它實際上是一個 web 視圖)中顯示三個圓形按鈕以提供一些操作。 當 CodeStream 面板停靠在右側時,3 個按鈕位於面板左側,這是正確的。
但是,如果用戶將 CodeStream 面板移動到屏幕的左側或其他地方,我希望能夠知道這一點,以便我可以指導我們的 web 視圖將 3 個按鈕移動到屏幕的右側。 我在下面提供了兩個屏幕截圖。
我們還有一個 VSCode 擴展,我在那里實現了類似的邏輯。
看一下IVsWindowFrame.GetFramePos方法:
using Microsoft.VisualStudio.Shell;
...
ThreadHelper.ThrowIfNotOnUIThread();
if (Package.GetGlobalService(typeof(Interop.IVsUIShell)) is Interop.IVsUIShell shellService)
{
shellService.GetToolWindowEnum(out var toolWindows);
foreach (Interop.IVsWindowFrame frame in ComUtilities.EnumerableFrom(toolWindows))
{
frame.GetProperty((int)VsFramePropID.DocView, out var docView);
if (docView?.GetType().FullName == "MyNamespace.MyToolWindow")
{
frame.GetFramePos(out var pos, out var guid, out int px, out int py, out int pcx, out int pcy);
// Do something
break;
}
}
}
我還沒有探索GetFramePos
提供的值的用法。 文檔說“返回窗口的位置”,所以大概它們對這個目的很有用。
(獲得目標Interop.IVsWindowFrame
可能有一種比上面示例中枚舉所有工具窗口更方便的方法......這正是我在發現此方法時所做的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.