Attribute VB_Name = "modEnumWindows"
Option Explicit

Private Type typEnumWin
    strWinText As String
    lngHwnd As Long
    blnVisible As Boolean
    blnEnabled As Boolean
End Type

Private objEnumWin() As typEnumWin

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function IsWindow Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal Hwnd As Long) As Long

Private Function fEnumWindowsProc(ByVal Hwnd As Long, ByVal lParam As Long) As Boolean
    Dim sWinText As String
    Dim lngWinTextLen As Long
    Dim bIsWin As Boolean
    Dim bIsVisible As Boolean
    Dim bIsEnabled As Boolean
    

    'See if Handle returns a Window ...
    bIsWin = (Not IsWindow(Hwnd) = 0)
    
    If bIsWin Then
        'See if Window is Visible ...
        bIsVisible = (Not IsWindowVisible(Hwnd) = 0)
        'See if Window is Enabled
        bIsEnabled = (Not IsWindowEnabled(Hwnd) = 0)
        'Get Window Text Length ...
        lngWinTextLen = GetWindowTextLength(Hwnd)
        'Get Window Text ...
        sWinText = Space(lngWinTextLen)
        GetWindowText Hwnd, sWinText, lngWinTextLen + 1
        
        If bIsVisible And bIsEnabled Then
            If Not objEnumWin(UBound(objEnumWin)).lngHwnd = 0 Then
                ReDim Preserve objEnumWin(UBound(objEnumWin) + 1)
            End If
            
            With objEnumWin(UBound(objEnumWin))
                .strWinText = sWinText
                .blnEnabled = bIsEnabled
                .blnVisible = bIsVisible
                .lngHwnd = Hwnd
            End With
        End If
        
    End If

    'List Sequence ...
    '1 : Window Handle
    '2 : Window Text
    '3 : bIsVisible
    '4 : bIsEnabled
    
    'Fill the ListView Control with Data ...
            
    'Continue same process ...
    fEnumWindowsProc = True
End Function

Private Function fGetWinInfo()
    'Call EnumWindowsProc ...
    EnumWindows AddressOf fEnumWindowsProc, ByVal 0&
End Function

Public Function fFindOtherWindowInstance(ByVal Hwnd As Long, ByVal WinText As String, ByRef DestHwnd As Long) As Integer
On Error Resume Next

Dim intFor As Integer
Dim intWinCount As Integer
Dim lngLastHwnd As Long
    
    ReDim objEnumWin(0)
    intWinCount = 0
    
    Call fGetWinInfo
    For intFor = LBound(objEnumWin) To UBound(objEnumWin)
        With objEnumWin(intFor)
            If .strWinText Like WinText And Not Hwnd = .lngHwnd Then
                intWinCount = intWinCount + 1
                lngLastHwnd = .lngHwnd
            End If
        End With
    Next intFor
    
    ReDim objEnumWin(0)
    
    If intWinCount = 1 Then
        DestHwnd = lngLastHwnd
    End If
    
    fFindOtherWindowInstance = intWinCount
    
End Function


