I need to list all child controls of a Windows, but in exacly the same order that appear in the window.
I have this code;
public static List<IntPtr> GetWindowControls(IntPtr hWnd)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindowControls);
EnumChildWindows(hWnd, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
private static bool EnumWindowControls(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
string className = Helpers.WinApi.GetWinClass(handle).ToUpper();
if (className.Contains("EDIT") || className.Contains("COMBOBOX") || className.Contains("STATIC") || className.Contains("BUTTON"))
list.Add(handle);
return true;
}
After the execution of that method, I get a Windows list, but with any order.
EnumChildWindows
does not give you the windows in the order you want. You will have to ask each window for its position and then order the windows yourself based on those positions.
You will need to decide what order to use. Top to bottom, then left to right. Or left to right, then to to bottom. Or perhaps some other order.
You can use GetWindowRect
to obtain the bounding rect for each window.