[英]How to retrieve a tool window's position in Visual Studio
In the Visual Studio extension I'm developing, I provide a custom tool window.在我正在开发的 Visual Studio 扩展中,我提供了一个自定义工具窗口。
I'm trying to find out if the user has the tool window docked to a side (left/right/top/bottom) or if it is floating.我试图找出用户是否将工具窗口停靠在一侧(左/右/上/下)或者它是否处于浮动状态。 Is there a way to calculate the current position of my tool window and is there an event to know after a user has completed moving it?
有没有办法计算我的工具窗口的当前位置,并且在用户完成移动它之后是否有要知道的事件?
Some background: I'm developing the CodeStream extension for Visual Studio.一些背景知识:我正在为 Visual Studio 开发 CodeStream 扩展。 There is functionality that when a user selects code we show three circle buttons in the CodeStream tool window (it's actually a webview) to provide some actions.
当用户选择代码时,我们会在 CodeStream 工具窗口(它实际上是一个 web 视图)中显示三个圆形按钮以提供一些操作。 When the CodeStream panel is docked to the right, the 3 buttons are on the left side of the panel and this is correct.
当 CodeStream 面板停靠在右侧时,3 个按钮位于面板左侧,这是正确的。
But, if a user moves the CodeStream panel to the left side of screen or elsewhere, I'd like to be able to know that so I can direct our webview to move the 3 buttons to the right side of the screen.但是,如果用户将 CodeStream 面板移动到屏幕的左侧或其他地方,我希望能够知道这一点,以便我可以指导我们的 web 视图将 3 个按钮移动到屏幕的右侧。 I've provided two screenshots below.
我在下面提供了两个屏幕截图。
We also have a VSCode extension and I've implemented similar logic there.我们还有一个 VSCode 扩展,我在那里实现了类似的逻辑。
Take a look at the IVsWindowFrame.GetFramePos method:看一下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;
}
}
}
I have not explored the usage of the values provided by GetFramePos
.我还没有探索
GetFramePos
提供的值的用法。 The docs say "Returns the position of the window," so presumably they are useful for that purpose.文档说“返回窗口的位置”,所以大概它们对这个目的很有用。
(There may be a more expedient way to obtain the target Interop.IVsWindowFrame
than enumerating all the tool windows as in the above example... that's just what I was doing when I discovered this method.) (获得目标
Interop.IVsWindowFrame
可能有一种比上面示例中枚举所有工具窗口更方便的方法......这正是我在发现此方法时所做的。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.