/// <summary> /// Iterate the windows, from top to bottom /// </summary> /// <param name="parent">InteropWindow as the parent, to iterate over the children, or null for all</param> /// <returns>IEnumerable with all the top level windows</returns> public static IEnumerable <IInteropWindow> GetTopWindows(IInteropWindow parent = null) { // TODO: Guard against looping var windowPtr = parent == null?User32Api.GetTopWindow(IntPtr.Zero) : User32Api.GetWindow(parent.Handle, GetWindowCommands.GW_CHILD); do { yield return(InteropWindowFactory.CreateFor(windowPtr)); windowPtr = User32Api.GetWindow(windowPtr, GetWindowCommands.GW_HWNDNEXT); } while (windowPtr != IntPtr.Zero); }