With "Process.GetProcesses()" way, you will get all the processes currently running on your system. And the main-window handle (
MainWindowHandle property of Process class) for each process. But if you want to get all the child-window (button, textbox, label etc) handle of each main-widnow, you can use the EnumChildWindow Win32 API. As example, if you want to find all the child-window, of the 'Calculator' process, you can use the following code...
// First declare a delegate of signature "EnumWindowsProc"
delegate int EnumWindowsProcDelegate(IntPtr hWnd, int lParam);
//Declare (through DllImport) signature for "EnumChildWindows" Win32 API
[DllImport("User32.Dll")]
public static extern int EnumChildWindows(IntPtr hParent, EnumWindowsProcDelegate lpEnumFunc, int lParam);
// Now let us use EnumChildWindows method to enumerate all the children of the "Calculator" process.
void UsageOfEnumChildWindows()
{
// You can use "EnumWindows" instead of Process.GetProcessByName or FindWindow API to find the handle of Calculator.
// EnumWindows enumerate through all top level windows. Perhaps .NET
// internally uses EnumWindows??? or WMI (Windows Management Instrumentation) ???
Process[] calcs = Process.GetProcessesByName("Calculator");
WindowsAPI.EnumWindowsProcDelegate lpdelegeate = new WindowsAPI.EnumWindowsProcDelegate(EnumChildProc);
WindowsAPI.EnumChildWindows(calcs[0].MainWindowHandle, lpdelegeate, 0);
}
// This is the method whether you will get each child-window handle.
public static int EnumChildProc(IntPtr hChildWnd, int lParam)
{
// hChildWnd is the handle to the child window of 'Calculator'
// you need to use hChildWnd here to meet your desire
// return 1 to continue enumerating through child windows; 0 to stop enumerating.
return 1;
}