1

I want to enumerate and iterate trough Child Windows of a program for UI Automation. My target is to be able to recognize if a special Window of the program is open or not by listing all windows and check for the title.

I am geting the Handle of the MainWindow by using this code and it works fine:

         foreach (Process pList in Process.GetProcesses())
         {
         listBox1.Items.Add(pList.MainWindowTitle);
         if (pList.MainWindowTitle.Contains("WindowName") && IsWindowVisible(pList.MainWindowHandle.ToInt32()))
            {
            Console.WriteLine(pList.MainWindowHandle);
            }
         }

But it recognize only the main Window but not other windows opend by this program. I already tried some code that I have googled, but still cant get the ChildWindow handles/titles.

For example this here:

    private void Form1_Load(object sender, EventArgs e)
    {
        Process[] processlist = Process.GetProcesses();

        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                if (process.Id == 8532)//this is chrome id for example/
                {
                    Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                    List<Process> a = GetChild(process);
                    foreach (Process p in a)
                    {
                        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                    }
                }
            }
        }
    }

    public static List<Process> GetChild(Process process)
    {
        List<Process> children = new List<Process>();
        ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));

        foreach (ManagementObject mo in mos.Get())
        {
            children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
        }

        return children;
    }

But I still don't get it how to access the ChildWindows.

halfer
  • 19,824
  • 17
  • 99
  • 186
Zer01
  • 41
  • 6
  • [WinEvents](https://msdn.microsoft.com/en-us/library/windows/desktop/dd373889.aspx). – IInspectable Oct 30 '17 at 17:52
  • How about enumerating the child windows given the main window? Haven't looked into the details but sounds possible: https://stackoverflow.com/questions/1363167/how-can-i-get-the-child-windows-of-a-window-given-its-hwnd. Determining if it's open might be tricky however. – kvr Oct 30 '17 at 20:22

0 Answers0