Word VSTO 加载项:在屏幕上获取工作区坐标/矩形

Word VSTO Add-In: Get Working Area Coordinates / Rectangle on Screen

使用 VSTO Word Interop 库,如何获取主要 "Working Area" 的屏幕坐标/矩形?即 LeftTopWidthHeight.

这张图片很好地显示了我要查找的区域,突出显示为 "DISPLAY" - 即包含文档的 panel/scroll-viewer。

我遇到了 ,它显示了一种与 Ranges 和 Window 本身有关的好方法,但深入研究了 Window / ActiveWindowViewActivePane,我找不到任何让我更接近我正在寻找的 "Working Area" 的属性。

C# 或 VBA 中的解决方案/方法会很棒。

Word 对象库只提供高度和宽度的信息:

Window.UsableHeight
Window.UsableWidth

它不提供 Word 应用程序 "editing" 部分的屏幕坐标,只提供整个应用程序 window。为此,我认为有必要使用 Windows API.

Cindy 对 Windows API 的友善指导使我走上了正确的轨道。

使用 System.Windows.Automation 命名空间和出色的 inspect.exe 工具,我能够隔离包含 document/working 区域的 ControlType

在实践中,Rect可以这样得到:

        var window = AutomationElement.FromHandle(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd));
        var panel = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
        var docRect = (Rect) panel.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty, true);