Option Explicit
' ***************************************************************************************************************
' Déclaration des Fonctions de l'API Windows
' ***************************************************************************************************************
'
#If Win64 Then
Declare PtrSafe Function Cherche_Fenêtre Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Declare PtrSafe Function EstVisible Lib "user32" Alias "IsWindowVisible" (ByVal Hwnd As Long) As Boolean
Declare PtrSafe Function EstFenêtre Lib "user32" Alias "IsWindow" (ByVal Hwnd As Long) As Boolean
Declare PtrSafe Function Get_Titre Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
#Else
Declare Function Cherche_Fenêtre Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Declare Function EstVisible Lib "user32" Alias "IsWindowVisible" (ByVal Hwnd As Long) As Boolean
Declare Function EstFenêtre Lib "user32" Alias "IsWindow" (ByVal Hwnd As Long) As Boolean
Declare Function Get_Titre Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
#End If
'Déclaration des variables liées aux fonctions
Private Const GW_HWNDNEXT As Long = 2
Sub test()
Dim Retour As Long
Dim Fenetre As String
Fenetre = "essai.bat" & Chr(160) & "- Bloc-notes"
Retour = RechercheFenêtre_Par_MotsClés(Fenetre, True)
If Retour = 0 Then
MsgBox Fenetre & " n'est pas ouvert"
Else
MsgBox Fenetre & " est ouvert"
End If
End Sub
Function RechercheFenêtre_Par_MotsClés(MotsDuTitres As String, Optional Bloque As Boolean) As Long
' Fonction qui renvoie le Handle d'une fenêtre Mère dont on ne donne qu'une partie du Titre
Dim Fenêtre As Long, NbOccurrences As Long, IdFenêtre As Long
Dim TitreFenêtre As String, AppelMacro As String
Dim Temp1 As String, temps2 As String
Fenêtre = Cherche_Fenêtre(0, 0) ' Recherche en Z, à partir de 0,0
IdFenêtre = 0
NbOccurrences = 0
Do While Fenêtre <> 0
If EstVisible(Fenêtre) And EstFenêtre(Fenêtre) Then
TitreFenêtre = RécupèreTitre(Fenêtre)
' For t = 1 To Len(MotsDuTitres)
' temps1 = Asc(Mid(TitreFenêtre, t, 1))
' temps2 = Asc(Mid(MotsDuTitres, t, 1))
' Next
If InStr(1, TitreFenêtre, MotsDuTitres) > 0 Then
IdFenêtre = Fenêtre ' récupération du Handle de la fenêtre
NbOccurrences = NbOccurrences + 1
End If
End If
' On passe à la Fenêtre suivante du même niveau (selon un parcours en Z)
Fenêtre = GetWindow(Fenêtre, GW_HWNDNEXT)
Loop
Select Case NbOccurrences
Case Is = 0
If Not Bloque Then
MsgBox ("Aucune fenêtre n'a été trouvée avec ces mots.")
Stop
End If
Case Is = 1
RechercheFenêtre_Par_MotsClés = IdFenêtre
Case Is > 1
If Not Bloque Then
MsgBox ("Plusieurs fenêtres ont été trouvées avec ces mots !")
Stop
End If
RechercheFenêtre_Par_MotsClés = IdFenêtre
End Select
End Function
Function RécupèreTitre(Handle As Long) As String
' Retourne le Titre de l'Objet (Fenêtre ou Champ)
Dim Titre As String
Titre = String(150, Chr$(0))
Get_Titre Handle, Titre, 150
RécupèreTitre = Left$(Titre, InStr(Titre, Chr$(0)) - 1)
End Function