Ejemplo n.º 1
0
 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        /// <summary>
        /// Enumerates all the hints from the given window
        /// </summary>
        /// <param name="hWnd">The window to get hints from</param>
        /// <param name="hintFactory">The factory to use to create each hint in the session</param>
        /// <returns>A hint session</returns>
        private HintSession EnumWindowHints(IntPtr hWnd, Func<IntPtr, Rect, AutomationElement, Hint> hintFactory)
        {
            var result = new List<Hint>();
            var elements = EnumElements(hWnd);

            // Window bounds
            var rawWindowBounds = new RECT();
            User32.GetWindowRect(hWnd, ref rawWindowBounds);
            Rect windowBounds = rawWindowBounds;

            foreach (AutomationElement element in elements)
            {
                var boundingRectObject = element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty, true);

                if (boundingRectObject == AutomationElement.NotSupported)
                {
                    // Not supported
                    continue;
                }

                var boundingRect = (Rect)boundingRectObject;
                if (boundingRect.IsEmpty)
                {
                    // Not currently displaying UI
                    continue;
                }

                // Convert the bounding rect to logical coords
                var logicalRect = boundingRect.PhysicalToLogicalRect(hWnd);
                if (!logicalRect.IsEmpty)
                {
                    var windowCoords = boundingRect.ScreenToWindowCoordinates(windowBounds);

                    var hint = hintFactory(hWnd, windowCoords, element);

                    if (hint != null)
                    {
                        result.Add(hint);
                    }
                }
            }

            return new HintSession
            {
                Hints = result,
                OwningWindow = hWnd,
                OwningWindowBounds = windowBounds,
            };
        }