Word VSTO 插件:获取工作区域坐标/屏幕上的矩形

问题描述 投票:0回答:3

使用VSTO Word Interop库,如何获取主“工作区域”的屏幕坐标/矩形?即

Left
Top
Width
Height

此图像很好地显示了我正在寻找的突出显示为“显示”的区域 - 即包含文档的面板/滚动查看器。

我遇到了这个答案,它展示了与

Range
s和
Window
本身有关的好方法,但是深入研究了
Window
/
ActiveWindow
View
ActivePane
,我无法找到任何能让我更接近我正在寻找的“工作区”的房产。

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

ms-word vsto office-interop office-addins word-interop
3个回答
4
投票

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

使用

System.Windows.Automation
命名空间和出色的
inspect.exe
工具,我能够隔离包含文档/工作区域的
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);

3
投票

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

Window.UsableHeight
Window.UsableWidth

它不提供 Word 应用程序“编辑”部分的屏幕坐标,仅提供整个应用程序窗口。为此,我认为有必要使用 Windows API。


0
投票

如果您现在想使用自动化

System.Windows.Automation
,因为对我来说它很重,您可以使用 user32 方法。

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
}
    
var wwFWindowHandle = FindWindowEx(parentWindowHandle, IntPtr.Zero, "_WwF", null);
if (wwFWindowHandle != IntPtr.Zero)
{
    var wwBWindowHandle = FindWindowEx(wwFWindowHandle, IntPtr.Zero, "_WwB", null);
    if (wwBWindowHandle != IntPtr.Zero)
    {
        var wwGWindowHandle = FindWindowEx(wwBWindowHandle, IntPtr.Zero, "_WwG", null);
        if (wwGWindowHandle != IntPtr.Zero)
        {
            GetWindowRect(wwGWindowHandle, out var newRect)
        }
    }
}

使用此代码您将获得页面区域。使用 Spy++ 您可以从 Word Office 中找到其他元素。

© www.soinside.com 2019 - 2024. All rights reserved.