public IntPtr GetTopElement()

in snipinsights/windows/SnipInsight/ImageCapture/SmartBoundaryDetection.cs [105:155]


        public IntPtr GetTopElement(int x, int y)
        {
            List<IntPtr> elementHnds = GetElementHndsInRange(_topElementHnds, x, y);
            if (elementHnds == null || elementHnds.Count == 0)
            {
                return IntPtr.Zero;
            }

            var minElementHandle = elementHnds[0];
            int minZOrder = int.MaxValue;
            foreach (var ohWnd in elementHnds)
            {
                int zOrder = 0;

                if (_windowZOrderMapping.TryGetValue(ohWnd, out zOrder))
                {
                    if (zOrder < minZOrder)
                    {
                        minZOrder = zOrder;
                        minElementHandle = ohWnd;
                    }
                }
            }

            // Check if the window is fully visible before looking for child windows
            if (minElementHandle == IntPtr.Zero)
                return IntPtr.Zero;

            var hdc = NativeMethods.GetDC(minElementHandle);
            if (hdc != IntPtr.Zero)
            {
                NativeMethods.RECT rcClip, rcClient;
                var ret = NativeMethods.GetClipBox(hdc, out rcClip);
                NativeMethods.GetClientRect(minElementHandle, out rcClient);
                NativeMethods.ReleaseDC(minElementHandle, hdc);
                if (ret == (int)NativeMethods.GetClipBoxReturn.SimpleRegion &&
                    rcClip.left == rcClient.left &&
                    rcClip.right == rcClient.right &&
                    rcClip.bottom == rcClient.bottom &&
                    rcClip.top == rcClient.top)
                {
                    IntPtr element = GetTopElement(minElementHandle, x, y);
                    if (element != IntPtr.Zero)
                    {
                        return element;
                    }
                }

            }
            return minElementHandle;
        }