Bonsoir, je cherchais une fonction permettant de lister les fenêtres attachées à un processus pour un script de gestion des fenêtres sous Windows.
Malheureusement, n'en ayant pas trouvé, j'ai dû me créer ma propre fonction ProcessGetWin (comme quoi, il semble manquer une fonction de ce nom ! ^^) pour le faire.
► Afficher le texte
Code : Tout sélectionner
#include <Array.au3>
Local $a = ProcessGetWin()
_ArrayDisplay($a)
Func ProcessGetWin($Whiteblack = 8, $sProcessGet = "", $sListed = "") ;User-Defined function that returns the Name of a Processus and his PID with the associated Windows and their Handles in a single 0-based array
;0Black-/1White-/2ZeroBased-/4Unique-/8ContainsWindow-, Process, -List
Local $aProcessList, $aProcessKeep, $aWinList, $aProcessGetWin, $aProcess, $i, $j, $k, $l, $aListed, $Found, $WinGetProcess, $Ubound
Dim $aProcessKeep[1][2], $aProcessGetWin[1][4]
$aProcessKeep[0][0] = 0
If BitAND($Whiteblack, 2) Then $Ubound = -1 ;ZeroBased
$aProcessGet = StringSplit($sProcessGet, ";")
$aListed = StringSplit($sListed, ";")
$aProcessList = ProcessList()
For $i = 1 To $aProcessList[0][0]
For $j = 1 To $aProcessGet[0]
If StringInStr($aProcessList[$i][0], $aProcessGet[$j]) > 0 Or $sProcessGet = "" Then ;if *process* exists
$aProcessKeep[0][0] += 1
ReDim $aProcessKeep[$aProcessKeep[0][0] + 1][2]
$aProcessKeep[$aProcessKeep[0][0]][0] = $aProcessList[$i][0]
$aProcessKeep[$aProcessKeep[0][0]][1] = $aProcessList[$i][1]
ExitLoop
EndIf
Next
Next
$aWinList = WinList()
If Not BitAND($Whiteblack, 8) Then ;ContainsWindow
$i = 1
While $i < $aWinList[0][0]
$h = WinGetPos($aWinList[$i][1])
If $aWinList[$i][0] = "" Or Not IsArray($h) Or $h[2] = 0 Or $h[3] = 0 Then
For $j = $i To $aWinList[0][0] - 1
For $k = 0 To UBound($aWinList, 2) - 1
$aWinList[$j][$k] = $aWinList[$j + 1][$k]
Next
Next
ReDim $aWinList[$aWinList[0][0]][UBound($aWinList, 2)]
$aWinList[0][0] -= 1
$i -= 1
EndIf
$i += 1
WEnd
EndIf
For $i = 1 To $aWinList[0][0] ;Parmi les processus existants
;~ If StringInStr($s & ";", ";" & $aWinList[$i][0] & ";") Then $Found = 1 ;inefficient
If $aWinList[$i][0] <> "" Then ;(one with a name...)
$Found = 0
$j = 1
While $j < UBound($aListed) And $Found = 0 ;List
If $aWinList[$i][0] = $aListed[$j] Then $Found = 1 ;Name
$j += 1
WEnd
If ($Found = 1 And BitAND($Whiteblack, 1)) Or ($Found = 0 And Not BitAND($Whiteblack, 1)) Then ;if (authorized) or (not forbidden)
$WinGetProcess = WinGetProcess($aWinList[$i][1])
For $j = 1 To $aProcessKeep[0][0] ;PID
If $aProcessKeep[$j][1] = $WinGetProcess Then
$Ubound += 1
ReDim $aProcessGetWin[$Ubound + 1][UBound($aProcessGetWin, 2)]
$aProcessGetWin[$Ubound][0] = $aProcessKeep[$j][0]
$aProcessGetWin[$Ubound][1] = $aProcessKeep[$j][1]
$aProcessGetWin[$Ubound][2] = $aWinList[$i][0]
$aProcessGetWin[$Ubound][3] = $aWinList[$i][1]
EndIf
Next
EndIf
EndIf
Next
If BitAND($Whiteblack, 4) Then ;Unique
$i = 1
While $i < $Ubound
$j = $i + 1
While $j <= $Ubound
If $aProcessGetWin[$i][0] = $aProcessGetWin[$j][0] Then
For $k = $j To $Ubound - 1
For $l = 0 To UBound($aProcessGetWin, 2) - 1
$aProcessGetWin[$k][$l] = $aProcessGetWin[$k + 1][$l]
Next
Next
ReDim $aProcessGetWin[$Ubound][UBound($aProcessGetWin, 2)]
$Ubound -= 1
$j -= 1 ;Retry
EndIf
$j += 1
WEnd
$i += 1
WEnd
EndIf
If Not BitAND($Whiteblack, 2) Then $aProcessGetWin[0][0] = $Ubound ;OneBased
Return $aProcessGetWin ;ProcName|PId|WinName|WinHndl
EndFunc
Fonctionnement :
ProcessGetWin(Mode, "Processus;cherché", "Liste;Des;Fenêtres")
Par défaut
ProcessGetWin(8, "", "") retourne un array contenant les fenêtres(+Handle) disponibles, et les processus(+PID) associés.
La recherche peut être rapidement affinée en utilisant par exemple ProcessGetWin(8, "explorer.exe;winamp") pour lister toutes les fenêtres attachées à des processus contenant ces mots, tout en ayant la possibilité d'en black-lister certaines pour ne pas les sélectionner.
Par-contre, cette fonction ne gère pas (encore) la recherche par PID, n'en ayant pas eu le besoin. Autrement, la gestion des White-/Black-Listes ne porte (pour le moment toujours) que sur les noms des fenêtres, il n'y a pas la possibilité de choisir les processus "tous sauf.." ni "portant exactement ce nom"...
Ouais, en gros, elle n'est pas finie, ok.

Elle existe, donc même si probablement incomplète, si vous en avez besoin, autant la partager !
D.