                       

Sub Form_Click ()
    Refresh
    CurrentX = 0
    CurrentY = 0
    
    FileName$ = GetFileName$((hWnd), h%)
    Print "File Name"; Tab(20); "= "; FileName$
    Print "Module Handle"; Tab(20); "= "; Hex$(h%)

    A% = GetInstanceCount%((hWnd))
    Print "Instance Count"; Tab(20); "= "; A%
    
    NextInstance% = FindSecondInstance%((hWnd))

    If NextInstance% Then
        w% = SetActiveWindow(NextInstance%)
    End If
    
    '|| Code to change Task List entry...
    'parenttext$ = "Hello " + parenttext$
    'y% = SetWindowText(GetWindow(hWnd, GW_OWNER), parenttext$)
End Sub


Function ActivateNextWindow% ()
    Owner% = GetWindow(hWnd, GW_OWNER)
    h% = GetClassWord(Owner%, GCW_HMODULE)
    Start$ = Space$(128)
    Other$ = Space$(128)
    y% = GetModuleFileName(h%, Start$, Len(Start$))
    Print Owner%, Start$
    A% = Owner%
    Do
        Other$ = Space$(128)
        A% = GetWindow(A%, GW_HWNDNEXT)
        If A% = 0 Then
            A% = GetWindow(Owner%, GW_HWNDFIRST)
        End If
        yH% = GetClassWord(A%, GCW_HMODULE)
        Handle% = A%
        yH% = GetModuleFileName(yH%, Other$, Len(Other$))
        Print " Found  = "; Hex$(A%); "  "; Other$
    Loop Until Other$ = Start$
    Print Hex$(Handle%); "  "; Other$
    ActivateNextWindow% = Handle%
End Function

Function GetFileName$ (WindowHandle As Integer, ModuleHandle As Integer)
    Dim FileName        As String
    Dim FileNameLength  As Integer

    If WindowHandle Then
        ModuleHandle = GetClassWord(WindowHandle, GCW_HMODULE)
    End If

    FileName = Space$(128)
    
    FileNameLength = GetModuleFileName(ModuleHandle, FileName, Len(FileName))
    
    FileName = Left$(FileName, FileNameLength)

    GetFileName$ = FileName
End Function

Function GetInstanceCount% (WindowHandle As Integer)
    Dim InstanceCount   As Integer
    Dim ModuleHandle    As Integer

    ModuleHandle = GetClassWord(WindowHandle, GCW_HMODULE)
    InstanceCount = GetModuleUsage(ModuleHandle)
    
    GetInstanceCount% = InstanceCount
End Function

Function FindSecondInstance% (WindowHandle As Integer)
    Dim WhoAreWe        As String
    Dim HowManyAreWe    As Integer
    Dim OurModule       As Integer

    Dim WhoAreWeAt      As String
    Dim SomeWindow      As Integer

    WhoAreWe = GetFileName$(WindowHandle, OurModule)
    HowManyAreWe = GetInstanceCount%(WindowHandle)

    If HowManyAreWe < 2 Then
        FindSecondInstance% = 0
        Exit Function
    End If

    SomeWindow = WindowHandle

    Do
        WhoAreWeAt = Space$(128)
        SomeWindow = GetWindow(SomeWindow, GW_HWNDNEXT)
        
        If SomeWindow = 0 Then SomeWindow = GetWindow(WindowHandle, GW_HWNDFIRST)

        WhoAreWeAt = GetFileName$((0), SomeWindow)
        
    Loop Until WhoAreWe = WhoAreWeAt

    FindSecondInstance% = SomeWindow
End Function

