Reputation: 1144
Using the VSTO Word Interop libraries, how can you get the screen coordinates / rectangle of the main "Working Area"? That is Left
, Top
, Width
and Height
.
This image quite nicely shows the area I am looking for highlighted as "DISPLAY" - that is the panel/scroll-viewer containing the document.
I came across this answer which shows a nice approach pertaining to Range
s and the Window
itself, but having dug into Window
/ ActiveWindow
, View
and ActivePane
, I was not able to find any properties that get me closer to the "Working Area" I am looking for.
A solution / approach in either C# or VBA would be great.
Upvotes: 3
Views: 655
Reputation: 43
If you do now want to use automation System.Windows.Automation
because for me it is heavy you can use user32 approach.
[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)
}
}
}
Using this code you will get pages area. Using Spy++ you can find other elements from word office.
Upvotes: 0
Reputation: 1144
Cindy's kind pointer to the Windows API got me on the right track.
Using the System.Windows.Automation
namespace and the excellent inspect.exe
tool, I was able to isolate the ControlType
containing the document/working area.
In practice, the Rect
can be obtained as follows:
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);
Upvotes: 4
Reputation: 25693
The Word object library provides only information for the height and width:
Window.UsableHeight
Window.UsableWidth
It provides nothing for the screen co-ordinates of the "editing" section of the Word application, only for the entire application window. For that, I think it will be necessary to work with the Windows API.
Upvotes: 3