tags:

views:

48

answers:

1

How do I collect all windows handlers in C#. I need all the windows (not just the parents) Thanks,

+3  A: 

Try the following utility class. Given a handle to a window it will return all of the associated child windows.

public class WindowFinder
{
    private class Helper
    {

        internal List<IntPtr> Windows = new List<IntPtr>();

        internal bool ProcessWindow(IntPtr handle, IntPtr parameter)
        {
            Windows.Add(handle);
            return true;
        }
    }

    private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowProc lpEnumFunc, IntPtr lParam);

    public static List<IntPtr> GetChildWindows(IntPtr parentWindow)
    {
        var helper = new Helper();
        EnumChildWindows(parentWindow, helper.ProcessWindow, IntPtr.Zero);
        return helper.Windows;
    }
}
JaredPar