|
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd _
As Long, ByVal wCmd 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 GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hwnd 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 GetParent Lib "user32" (ByVal hwnd _
As Long) As Long
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Public Function FindWindowLike(ByRef hWndArray() As Variant, _
ByVal hWndStart As Long, ByVal WindowText As String, _
ByVal Classname As String) As Long
Static nLevel As Long
Static nCounter As Long
Dim hwnd As Long
Dim hwndMain As Long
Dim nRetVal As Long
Dim strWndTitle As String
Dim strClassName As String
Dim strWndType As String
Dim strKey As String
If nLevel = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
nCounter = -1
ReDim hWndArray(0 To 4, 0 To 0)
End If
nLevel = nLevel + 1
hwnd = GetWindow(hWndStart, GW_CHILD)
Do Until hwnd = 0
nRetVal = FindWindowLike(hWndArray(), hwnd, WindowText, _
Classname)
strClassName = Space$(64)
nRetVal = GetClassName(hwnd, strClassName, Len(strClassName))
strClassName = Left$(strClassName, nRetVal)
nRetVal = GetWindowTextLength(hwnd)
If nRetVal > 0 Then
If nRetVal > 256 Then nRetVal = 255
strWndTitle = Space$(nRetVal + 1)
GetWindowText hwnd, strWndTitle, Len(strWndTitle)
strWndTitle = Left$(strWndTitle, nRetVal)
Else
strWndTitle = ""
End If
If (UCase$(strWndTitle) Like UCase$(WindowText)) And _
(UCase$(strClassName) Like UCase$(Classname)) Then
If GetParent(hwnd) <> 0 Then
nRetVal = GetParent(hwnd)
Do While nRetVal
hwndMain = nRetVal
nRetVal = GetParent(hwndMain)
Loop
strWndType = "Child"
strKey = CStr(hwndMain) & "h" & CStr(nCounter)
Else
strWndType = "Parent"
strKey = CStr(hwnd) & "h" & CStr(nCounter)
End If
nCounter = nCounter + 1
ReDim Preserve hWndArray(0 To 4, 0 To nCounter)
hWndArray(0, nCounter) = strWndTitle
hWndArray(1, nCounter) = CStr(hwnd)
hWndArray(2, nCounter) = strClassName
hWndArray(3, nCounter) = strWndType
hWndArray(4, nCounter) = strKey
End If
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
nLevel = nLevel - 1
FindWindowLike = nCounter + 1
End Function
|
|