views:

600

answers:

4

I have the handle for a given window. How can I enumerate its child windows?

A: 

Using:

internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);

[DllImport("user32.dll")]
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

you will get callbacks on the function you pass in.

Grzenio
A: 

Use EnumChildWindows, with p/invoke. Here's an interesting link about some of it's behavior: http://blogs.msdn.com/oldnewthing/archive/2007/01/16/1478717.aspx

If you don't know the handle of the window, but only it's title, you'll need to use EnumWindows. http://pinvoke.net/default.aspx/user32/EnumWindows.html

John Fisher
+1  A: 

Here is a managed alternative to EnumWindows, but you will still need to use EnumChildWindows to find the handle of the child window.

foreach (Process process in Process.GetProcesses())
{
   if (process.MainWindowTitle == "Title to find")
   {
      IntPtr handle = process.MainWindowHandle;

      // Use EnumChildWindows on handle ...
   }
}
Special Touch
A: 

I've found the best solution to be Managed WindowsAPI. It had a CrossHair control that could be used to select a window(not part of the question), and a method AllChildWindows to get all child windows which likely wrapped the EnumChildWindows function. Better not to reinvent the wheel.

Yuriy Faktorovich