1

The problem statement is our required application will be running on a remote machine we user will be using that machine via Remote Desktop Connection. The idea is to take screenshots of the application area only, running on that machine. We are able to get application window rectangular bounds via spyxx, window handle is returning correct for the window and processId is accessible but when we are trying to get rectangular bounds we are getting some wrong coordinates. Any help would be appreciated.

var winhandle = NativeMethods.FindWindow("RAIL_WINDOW", null);
            if (winhandle != IntPtr.Zero)
            {
                var mainEMRWindow = AutomationElement.FromHandle(winhandle);
                if (mainEMRWindow != null)
                {
                   Console.WriteLine("Bounding Rectangle: " + mainEMRWindow.Current.BoundingRectangle.Left + "," + mainEMRWindow.Current.BoundingRectangle.Top + "," + mainEMRWindow.Current.BoundingRectangle.Right + "," + mainEMRWindow.Current.BoundingRectangle.Bottom);
                                           RECT clientRect = GetClientRect(winhandle);

                    Console.WriteLine("Client Rect: " + "Left: " + clientRect.Left.ToString() + "," + "Top: " + clientRect.Top.ToString() + "," + "Right: " + clientRect.Right.ToString() + "," + "Bottom: " + clientRect.Bottom.ToString());

                    Rectangle rc;
                    GetWindowRect(winhandle, out rc);

                    Console.WriteLine("Window Rect: " + "Left: " + rc.Left.ToString() + "," + "Top: " + rc.Top.ToString() + "," + "Right: " + rc.Right.ToString() + "," + "Bottom: " + rc.Bottom.ToString());
                }
            }

I am going to attach the screenshot of the application and code as well. DPI Aware is Per Monitor. Correct Bounding Rectangle is Left 65, Top 10, Right 1793, and bottom 1020 in this case but I'm getting Bounding Rectangle 105, 568, 1108,594 which is wrong.

enter image description here

enter image description here

  • You forgot to mention what are the *wrong* measures you get and from what method: UI Automation, `GetWindowRect()` or `GetClientRect()` (which of course returns the ClientRectangle and not the Window Bounds) and how these measures differ from what you see in Inspect (is the latter to be considered the *right* value?). Note that these functions are not Dpi Aware. Try with `DwmGetWindowAttribute()`. Take the code from [here](https://stackoverflow.com/a/48812831/7444103). You also didn't mention the DpiAwareness status of this application. – Jimi Mar 17 '21 at 13:14
  • DPI Aware is Per Monitor, Wrong measures screenshot is attached and I need to get rectangle bounds. Also I am going to try your provided solution and surely will update you about my findings. – Farrukh Niaz Mar 17 '21 at 15:02
  • Instead of calling FindWindow, use e.g., FindWindowEx or, since you're using UI Automation, use the automation methods. e.g., `var railWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new AndCondition(new[] { new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window), new PropertyCondition(AutomationElement.ClassNameProperty, "RAIL_WINDOW")}));`. The measure you get seem to belong to another (quite smaller) Window. – Jimi Mar 17 '21 at 15:30
  • This one worked for me. Thanks @Jimi. – Farrukh Niaz Mar 18 '21 at 04:05

1 Answers1

0

@Jimi was absolutely right. I was getting some wrong window measures but from same process and same window handle. It worked for me by using this var railWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new AndCondition(new[] { new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window), new PropertyCondition(AutomationElement.ClassNameProperty, "RAIL_WINDOW")}));