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.