![](/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.