Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.Net equivalent of enumerating child windows with the API

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Hello all,

What is the .Net equivalent to enumerating child windows using the Win32 API? Thanks in advance for any ideas and/or suggestions!
 
SBendBuckeye,

In order to communicate with the other application's windows, we will need to define some constants:
Code:
Const GW_CHILD As Integer = 5
Const GW_HWNDNEXT As Integer = 2

The constants above enable us to enumerate through a collection of child windows based on the handle of the parent window.

Now we need a function to return the handle to a window based on its relationship to another window:
Code:
Declare Auto Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Long) As IntPtr

Finally we need a function to enumerate the handles for all child windows (given the handle to the parent window):
Code:
Public Function GetWindows(ByVal ParentWindowHandle As IntPtr) As IntPtr()

    Dim ptrChild As IntPtr
    Dim ptrRet() As IntPtr
    Dim iCounter As Integer

    'get first child handle...
    ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)

    'loop through and collect all child window handles...
    Do Until ptrChild.Equals(IntPtr.Zero)
       'process child...
       ReDim Preserve ptrRet(iCounter)
       ptrRet(iCounter) = ptrChild
       'get next child...
       ptrChild = GetWindow(ptrChild, GW_HWNDNEXT)
       iCounter += 1
    Loop

    'return...
    Return ptrRet

End Function

Based on your question I hope this at least leads you in the right direction.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"In the new millennium there will be two kinds of business; those on the net and those out of business"

~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thanks Psycho, so I still need to use the Win32 API as before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top