<!--StartFragment --> ' Déclaration des API utilisées
Public Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch
As Long) As Long
Public Declare Function EnumWindows Lib
"user32" _
(ByVal lpEnumFunc As Long, ByVal lParam
As Long) As Long
' Variable temporaire utilisée uniquement par la fonction GetWindowsList
' pour stocker les fenêtres.
Public stGetWindowsList
As String
' Exemple d'utilisation :
' Dim tabFenetre
' tabFenetre = GetWindowsList
Public Function
GetWindowsList()
' Cette fonction retourne sous forme de tableau l'ensemble
' des titres des fenêtres de premier niveau ouvertes sous windows.
Dim lgRep
As Long
stGetWindowsList = vbNullString
' Appel de l'API et envoi du pointeur vers notre fonction de rappel
lgRep = EnumWindows(AddressOf EnumWindowsProc, 0)
' Découpage de la chaîne pour retourner un tableau
GetWindowsList = Split(stGetWindowsList, vbCrLf)
End Function
' Déclaration de la fonction de rappel
Public Function EnumWindowsProc(ByVal lgHwnd As Long, ByVal lgParam
As Long) As Long
Dim stTmp As String, lgTmp As Long, lgRet
As Long
stTmp = Space$(120)
lgTmp = 119
' On récupère le titre de la fenêtre à partir du handle
lgRet = GetWindowText(lgHwnd, stTmp, lgTmp)
stTmp = Replace(stTmp, Chr$(0), vbNullString)
' Stockage du résultat dans la chaine temporaire (ajout au texte existant).
' On pourrait imaginer construire une chaîne plus complexe en ajoutant également le handle
' de la fenêtre, ce qui permettrait des manipulations externes (comme une fermeture par exemple).
If (Trim$(stTmp) <> vbNullString) Then
stGetWindowsList = stGetWindowsList & stTmp & vbCrLf
' Retourne 1 systématiquement
EnumWindowsProc = 1
End Function