Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the child windows of a window given its HWND?

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

like image 612
Yuriy Faktorovich Avatar asked Sep 01 '09 15:09

Yuriy Faktorovich


People also ask

How do I find my child's windows?

You have to call FindWindowEx() for each child level that you want to go down, specifying the HWND found in the previous level as the parent, eg: HWND hWnd = FindWindow("XYZ_Widget_1", NULL); if (hWnd != NULL) { hWnd = FindWindowEx(hWnd, NULL, "XYZ_Widget_0", NULL); if (hWnd !=

How do you get a child's window handle?

Iterate through child windows. Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

What is a child window in windows?

A window that opens inside a parent window is a child window. The action taken on the parent window reflects on the child window as well. For example, if you close the parent window, the child window closes too.

How do you close the child window when the parent closes?

If the goal is to close the child window when the parent is closed or refreshed, rather than actually detecting the event, you can use a setInterval to detect when the window.


2 Answers

Here you have a working solution:

public class WindowHandleInfo {     private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);      [DllImport("user32")]     [return: MarshalAs(UnmanagedType.Bool)]     private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);      private IntPtr _MainHandle;      public WindowHandleInfo(IntPtr handle)     {         this._MainHandle = handle;     }      public List<IntPtr> GetAllChildHandles()     {         List<IntPtr> childHandles = new List<IntPtr>();          GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles);         IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList);          try         {             EnumWindowProc childProc = new EnumWindowProc(EnumWindow);             EnumChildWindows(this._MainHandle, childProc, pointerChildHandlesList);         }         finally         {             gcChildhandlesList.Free();         }          return childHandles;     }      private bool EnumWindow(IntPtr hWnd, IntPtr lParam)     {         GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam);          if (gcChildhandlesList == null || gcChildhandlesList.Target == null)         {             return false;         }          List<IntPtr> childHandles = gcChildhandlesList.Target as List<IntPtr>;         childHandles.Add(hWnd);          return true;     } } 

How to consume it:

class Program {     [DllImport("user32.dll", EntryPoint = "FindWindowEx")]     public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);      static void Main(string[] args)     {         Process[] anotherApps = Process.GetProcessesByName("AnotherApp");         if (anotherApps.Length == 0) return;         if (anotherApps[0] != null)         {             var allChildWindows = new WindowHandleInfo(anotherApps[0].MainWindowHandle).GetAllChildHandles();         }     } } 
like image 76
Caffé Avatar answered Sep 21 '22 16:09

Caffé


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.

like image 23
Grzenio Avatar answered Sep 18 '22 16:09

Grzenio