我想枚举并迭代用于UI自动化程序的子窗口。我的目标是通过列出所有窗口并检查标题来识别程序的特殊窗口是否打开。
我正在使用此代码来处理MainWindow的Handle,它工作正常:
foreach (Process pList in Process.GetProcesses())
{
listBox1.Items.Add(pList.MainWindowTitle);
if (pList.MainWindowTitle.Contains("WindowName") && IsWindowVisible(pList.MainWindowHandle.ToInt32()))
{
Console.WriteLine(pList.MainWindowHandle);
}
}
但它只识别主窗口,但不识别此程序打开的其他窗口。 我已经尝试了一些我用Google搜索的代码,但仍然无法获得ChildWindow句柄/标题。
例如这里:
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;
}
但我仍然不知道如何访问ChildWindows。