Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all child windows of window

How can I extract all child windows of a window?

Sample code:

Dim hWnd As IntPtr = ProcessName.MainWindowHandle
For Each hWndChild As IntPtr In hWnd
  MsgBox(hWndChild.classname.tostring & ", Caption: " & hWndChild.caption.tostring())
Next

(like spy++)

like image 938
famf Avatar asked Oct 06 '22 00:10

famf


1 Answers

Just as others have suggested, you should use the EnumWindows and EnumChildWindows functions.

Here's a link to little demonstration program I just ported from pieces of code from another program I had written in VB6 (a very long time ago): Windows Scanner

Hope it helps...


Edit: I just realized this wasn't much of an answer without actually explaining how those functions work. So, here it goes:

The EnumWindows function takes as its first parameters a pointer to a callback function. The second parameter is a value that you can pass to the callback function. You can think of it as a user-defined argument.

Every time EnumWindows "finds" a new window, it will call the callback function to inform about the new window. This callback function takes as parameters, the handler of the window and the optional parameter that the user specified when EnumWindows was first called.

So, basically, this is how you call EnumWindows:

EnumWindows(New EnumWindowsProc(AddressOf EnumProc), 0)

Where EnumWindowsProc is a delegate used to create a reference to the EnumProc function, which will be our callback.

The signature of such callback is as follows:

Private Function EnumProc(hWnd As IntPtr, lParam As IntPtr) As Boolean

It is inside this function that you populate your internal array of discovered windows.

Things are pretty much the same for the EnumChildWindows function, with the only difference being that its first parameter must be the handler of the parent window. Everything else is handled in the exact same way.

If you check the source code of the WindowsScanner program, you will see that I even use the same delegate and the same callback function for both EnumWindows and EnumChildWindows. So how do I know if we are enumerating top-level or child windows? Easy, I simply set the last parameter of EnumChildWindows to "1". Then, this parameter is passed to the callback function (EnumProc) which allows it to take different actions based on the value of that parameter.

like image 52
xfx Avatar answered Oct 10 '22 03:10

xfx