/// <summary> /// Enumerates all the child windows of a parent window /// </summary> /// <param name="parentHWnd">the handle to the parent window</param> /// <returns>list of WindowInfo structures containing the data of the child windows</returns> public static WindowInfo[] EnumChildWindows(IntPtr parentHWnd) { System.Collections.ArrayList childWindowArray = new System.Collections.ArrayList (10); IntPtr lastChildHWnd = FindWindowExA (parentHWnd, IntPtr.Zero, null, null); while (lastChildHWnd != IntPtr.Zero) { childWindowArray.Add (GetWindowInfo (lastChildHWnd)); lastChildHWnd = FindWindowExA (parentHWnd, lastChildHWnd, null, null); } WindowInfo[] returnArray = new WindowInfo[childWindowArray.Count]; for (int i = 0; i < childWindowArray.Count; i++) { returnArray[i] = (WindowInfo) childWindowArray[i]; } return returnArray; }
/// <summary> /// Matches a specific window environment against a specified window and its childs /// -- Might throw a HolodeckExceptions.MismatchingWindowEnvironmentException /// </summary> /// <param name="info">the window info to match against the window environment</param> /// <param name="environment">the window environment to match against</param> /// <returns>the handler to the matched window (specified by the environment), or null if no such window was found</returns> protected static IntPtr MatchEnvironment(WindowInfo info, WindowEnvironment environment) { IntPtr searchedHandle = IntPtr.Zero; WindowInfo[] array = EnumChildWindows (info.hWnd); bool matchTitle = (!environment.checkTitle || (environment.checkTitle && (info.title == environment.title))); bool matchChildSize = (((environment.childs == null) && (array.Length == 0)) || (environment.childs.Length == array.Length)); if (matchTitle && matchChildSize) { if (environment.searchedHandle) { // We might have found our window, but we also need to check the child environments. // Store our handle in searchedHandle for now. searchedHandle = info.hWnd; } if (environment.childs != null) { for (int i = 0; i < array.Length; i++) { IntPtr retHandle = MatchEnvironment (array[i], environment.childs[i]); // MatchEnvironment might throw an exception, but we don't need to worry about it here if (retHandle != IntPtr.Zero) { searchedHandle = retHandle; } } } // If we got here, that means that all the child environments matched as well. // searchHandle is not IntPtr.Zero if we've found our window. return searchedHandle; } else { // We need to throw this to differentiate from matching sub-environments that do not contain the searched window handle. throw new HolodeckExceptions.MismatchingWindowEnvironmentException ("The window doesn't match the specified environment"); } }
/// <summary> /// Searches for a label containing the specified string in a given window and in all its child windows /// </summary> /// <param name="info">the WindowInfo containing the handle to a window</param> /// <param name="labelSubString">the string to look for in the labels</param> /// <returns>the full text of the label if it was found, or string.Empty otherwise</returns> protected static string SearchForLabel(WindowInfo info, string labelSubString) { if (info.title.IndexOf (labelSubString) != -1) { // We've found the specified label return info.title; } WindowInfo[] array = EnumChildWindows (info.hWnd); foreach (WindowInfo tempInfo in array) { string title = SearchForLabel (tempInfo, labelSubString); if (title != string.Empty) { // One of our child window was the specified label return title; } } // None of our child window had the specified label return string.Empty; }