简体   繁体   English

如何在 Visual Studio 中检索工具窗口的位置

[英]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.

相关问题 如何在 Visual Studio 中以编程方式在第一个位置显示自定义工具窗口 - How to display custom tool window at the first position, programmatically in visual studio 如何从 Visual Studio 扩展显示弹出 window(不是工具窗口)? - How to display popup window (not tool window) from Visual Studio extension? 如何以编程方式打开Visual Studio扩展的工具窗口? - How to open a tool window of a visual studio extension programmatically? 如何在 Visual Studio 中寻址/调用我的自定义工具 window - How to adress/ call my custom tool window in Visual Studio 如何获得Visual Studio 2008编辑器窗口的topLeft屏幕位置? - How to get the topLeft screen position of Visual studio 2008's editor window? 如何在Visual Studio 2015/2017的自定义工具窗口中创建窗口表单? - How to create window form inside custom tool window in visual studio 2015/2017? 安装了Visual Studio工具窗口扩展,但看不到窗口 - Visual Studio Tool Window Extension installs but window can't be seen Visual Studio扩展:如何在选项卡链接组中找到哪个工具窗口位于前台? - Visual Studio extension: How do I find which tool window is in the foreground in a tab link group? 将两个或多个工具窗口添加到同一个Visual Studio项目中 - Add two or more Tool Window into the same Visual Studio project 在Visual Studio中自动隐藏工具窗口的快捷方式是什么? - What is the shortcut to auto hide a Tool Window in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM