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.