Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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 GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Sub Timer1_Timer()
Dim pt32 As POINTAPI
Dim sWindowText As String * 100, sWindowLen As Integer
Dim sClassName As String * 100, sClassLen As Integer
Dim hWndOver As Long
Dim hWndParent As Long
Dim sParentClassName As String * 100, sParentWindowLen As Integer
Dim sParentWindowText As String * 100, sParentClassLen As Integer
Static hWndLast As Long
If GetAsyncKeyState(17) Then
Call GetCursorPos(pt32)
hWndOver = WindowFromPointXY(pt32.x, pt32.y)
If hWndOver <> hWndLast Then
hWndLast = hWndOver
Debug.Print "-------------------------" & vbCrLf & "Window Info:"
Debug.Print "Window Handle: &H"; Hex(hWndOver)
sWindowLen = GetWindowText(hWndOver, sWindowText, 100)
Debug.Print "Window Text: " & Left(sWindowText, sWindowLen)
sClassLen = GetClassName(hWndOver, sClassName, 100)
Debug.Print "Window Class Name: "; Left(sClassName, sClassLen)
hWndParent = GetParent(hWndOver)
Dim parentHandles() As Long, parentCount As Integer
Debug.Print vbCrLf & "#################################################" & vbCrLf & "Dim hWnd as Long"
While hWndParent <> 0
ReDim Preserve parentHandles(parentCount)
parentHandles(parentCount) = hWndParent
hWndParent = GetParent(hWndParent)
parentCount = parentCount + 1
Wend
Dim printStr As String
For i = parentCount - 1 To 0 Step -1
sParentWindowLen = GetWindowText(parentHandles(i), sParentWindowText, 100)
sParentClassLen = GetClassName(parentHandles(i), sParentClassName, 100)
If i = parentCount - 1 Then
printStr = "hWnd = FindWindow(" & Chr(34) & Left(sParentClassName, sParentClassLen) & Chr(34) & ", " & Chr(34) & Left(sParentWindowText, sParentWindowLen) & Chr(34) & ")"
Else
printStr = "hWnd = FindWindowEx(hWnd, 0&, " & Chr(34) & Left(sParentClassName, sParentClassLen) & Chr(34) & ", " & Chr(34) & Left(sParentWindowText, sParentWindowLen) & Chr(34) & ")"
End If
Debug.Print Replace(Replace(printStr, Chr(34) & Chr(34) & ",", "0&,"), Chr(34) & Chr(34), "vbNullString")
Next
If parentCount = 0 Then
printStr = "hWnd = FindWindow(" & Chr(34) & Left(sClassName, sClassLen) & Chr(34) & ", " & Chr(34) & Left(sWindowText, sWindowLen) & Chr(34) & ")"
Else
printStr = "hWnd = FindWindowEx(hWnd, 0&, " & Chr(34) & Left(sClassName, sClassLen) & Chr(34) & ", " & Chr(34) & Left(sWindowText, sWindowLen) & Chr(34) & ")"
End If
Debug.Print Replace(Replace(printStr, Chr(34) & Chr(34) & ",", "0&,"), Chr(34) & Chr(34), "vbNullString")
Debug.Print "#################################################" & vbCrLf & "-------------------------" & vbCrLf
End If
End If
End Sub