Hi everyone,

I am unable to identify the child windows of any application. this code identifies the parent window propely but for child window it gives wrong output. Can someone help me with this? It will be really helpful.



Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal _
xPoint As Long, ByVal yPoint As Long) As Long


Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal Hwnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal Hwnd As Long, ByVal lpClass _
As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal Hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As Long, lpRect As RECT) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal Hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal Hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0 'X Size of screen
Const SM_CYSCREEN = 1 'Y Size of Screen
Const SM_CYCAPTION = 4 'Height of windows caption

Private Type POINTAPI
X As Long
Y As Long

End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private gStop As Boolean
Private prevWindow As Long, curWindow As Long, childWindow As Long
Private className As String
Private retValue As Long
Private mousePT As POINTAPI
Private mlLastWindow As Long

Const GWL_STYLE = (-16)


Public Sub Timer1_Timer()

Dim mouseposition As POINTAPI
Dim lWindowStyle As Long
Dim Length As Long
Dim wntxt As String
Dim rval As Long
Dim rRect As RECT
Dim newX As Long
Dim newY As Long
Dim newX1 As Long
Dim newY1 As Long
Dim mypoint As Point


' To check left mouse button pass 1,
If GetAsyncKeyState(1) = 0 Then
mousests.Text = "Your Left Mouse Button Is UP"
handletxt.Text = ""
classtxt.Text = ""
styletxt.Text = ""
captiontxt.Text = ""

Else

' Capture the coordinates where the mouse is clicked

Call GetCursorPos(mouseposition)
mousests.Text = "Left Mouse Button Down at X: " & mouseposition.X & " Y: " & mouseposition.Y

' Retrieve the window handle at the given coordinate position

curWindow = WindowFromPoint(mouseposition.X, mouseposition.Y)

GetWindowRect curWindow, rRect

newX1 = rRect.Left - mouseposition.X
newY1 = rRect.Top - mouseposition.Y

If newX1 < 0 Then
newX1 = newX1 * -1
End If
If newY1 < 0 Then
newY1 = newY1 * -1
End If

childWindow = ChildWindowFromPoint(curWindow, newX1, newY1)

While (curWindow <> childWindow And childWindow <> 0)
curWindow = childWindow
GetWindowRect curWindow, rRect

newX = rRect.Left - mouseposition.X
newY = rRect.Top - mouseposition.Y
If newX < 0 Then
newX = newX * -1
End If
If newY < 0 Then
newY = newY * -1
End If

childWindow = ChildWindowFromPoint(curWindow, newX, newY)

Wend

handletxt.Text = Hex(curWindow)

' Display window class
className = String$(256, " ")
retValue = GetClassName(curWindow, className, 255)
className = Left$(className, InStr(className, _
vbNullChar) - 1)
classtxt.Text = className

' Display window style

lWindowStyle = GetWindowLong(curWindow, GWL_STYLE) ' GWL_EXSTYLE
styletxt.Text = Hex(lWindowStyle)

' Get window caption

Length = GetWindowTextLength(curWindow) + 1
wntxt = Space(Length)
rval = GetWindowText(curWindow, wntxt, Length) 'Get title bar text
wntxt = Left(wntxt, Length - 1)

If wntxt = "" Then
captiontxt.Text = "NA"
Else
captiontxt.Text = wntxt
End If

End If
End Sub

Thanks.