Suite à mon exemple précédent qui permet de récupérer les infos sur les contrôles d'une fenêtre , j'ai eu envie de pousser un peu plus loin en créant un "Finder Tool" embarqué, c'est à dire intégrer le Finder Tool de l'utilitaire AutoIt Window Info dans une GUI.
Quelques morceaux ont été récupérés du Form Captor
Bon c'est sûr ça ne vaut pas AutoIt Window Info... et c'est surtout moins pro !
Exemple
► Afficher le texte
Code : Tout sélectionner
#Include "finderTool.au3"
$hGUI = GUICreate("Embeded Finder Tool", 300, 80, -1, -1, -1, $WS_EX_TOPMOST )
GUISetState()
$hTargetPic = _FormCaptor_Create(24, 20) ; Creates the PIC
_FormCaptor_Init() ; Initialize then Captor
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $hTargetPic
_FormCaptor_SetCapture()
EndSwitch
If _FormCaptor_Action() Then
MsgBox(0, "", _
"HANDLE : " & $aHwndCtrlInfo[0] & @CRLF & _
"CLASS : " & $aHwndCtrlInfo[1] & @CRLF & _
"Instance : " & $aHwndCtrlInfo[2] & @CRLF & _
"ClasseNameNN : " & $aHwndCtrlInfo[3] & @CRLF & _
"Advanced Mode : " & $aHwndCtrlInfo[4] & @CRLF & _
"ID : " & $aHwndCtrlInfo[5] & @CRLF & _
"Text : " & $aHwndCtrlInfo[6] & @CRLF & _
"Position X : " & $aHwndCtrlInfo[7] & @CRLF & _
"Position Y : " & $aHwndCtrlInfo[8] & @CRLF & _
"Largeur : " & $aHwndCtrlInfo[9] & @CRLF & _
"Hauteur : " & $aHwndCtrlInfo[10] & @CRLF & _
"Style : " & $aHwndCtrlInfo[11] & @CRLF & _
"Style étendu : " & $aHwndCtrlInfo[12] & @CRLF & _
"Position X (screen): " & $aHwndCtrlInfo[13] & @CRLF & _
"Position Y (screen): " & $aHwndCtrlInfo[14] )
EndIf
Wend
► Afficher le texte
Code : Tout sélectionner
#Include-once
#include <Array.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $aHwndCtrlInfo ; Tableau qui contient les infos sur l'élément sélectionné
Global $iAction = 0
Global $FormCaptor_State = False, $gOldCursor, $iFormCaptor
Global $hOldWindow, $hOldControl, $frame[4]
Global $cols = 15, $layerColor = 0xFF0000, $lineSize = 3
Global $showLayer = True
Global $CURSOR_TARGET = _WriteResource( _
"0x000002000100202000000F001000300100001600000028000000200000004000000001000100000000008000" & _
"00000000000000000000020000000200000000000000FFFFFF0000000000000000000000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"00000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFF83FFFFFE6CFFFFFD837FFFFBEFBFFFF783DFFFF7EFDFFFEAC6AFFFEABAAFFFE0280FFFEABAAFFFEAC6A" & _
"FFFF7EFDFFFF783DFFFFBEFBFFFFD837FFFFE6CFFFFFF83FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFFFFF")
Global $ICON_TARGET_FULL = _WriteResource( _
"0x0000010001002020080000000000E80200001600000028000000200000004000000001000400000000000002" & _
"000000000000000000001000000010000000000000000000800000800000008080008000000080008000808000" & _
"00C0C0C000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF000000000000000000" & _
"00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFF00000FFFFFFFFFFFF000FFFFFFFFFF00FF0FF00FFFFFFFFFF000FF" & _
"FFFFFFF0FF00000FF0FFFFFFFFF000FFFFFFFF0FFFFF0FFFFF0FFFFFFFF000FFFFFFF0FFFF00000FFFF0FFFFFF" & _
"F000FFFFFFF0FFFFFF0FFFFFF0FFFFFFF000FFFFFF0F0F0FF000FF0F0F0FFFFFF000FFFFFF0F0F0F0FFF0F0F0F" & _
"0FFFFFF000FFFFFF0000000F0F0000000FFFFFF000FFFFFF0F0F0F0FFF0F0F0F0FFFFFF000FFFFFF0F0F0FF000" & _
"FF0F0F0FFFFFF000FFFFFFF0FFFFFF0FFFFFF0FFFFFFF000FFFFFFF0FFFF00000FFFF0FFFFFFF000FFFFFFFF0F" & _
"FFFF0FFFFF0FFFFFFFF000FFFFFFFFF0FF00000FF0FFFFFFFFF000FFFFFFFFFF00FF0FF00FFFFFFFFFF000FFFF" & _
"FFFFFFFF00000FFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF0" & _
"00FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000000007770CCCCCCCCCCCCCCCCCCCC" & _
"C07770007070CCCCCCCCCCCCCCCCCCCCC07070007770CCCCCCCCCCCCCCCCCCCCC0777000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000FFFFFFFF8000000080000000800000008000000080000000800000008000000080000000" & _
"800000008000000080000000800000008000000080000000800000008000000080000000800000008000000080" & _
"0000008000000080000000800000008000000080000000800000008000000080000000FFFFFFFFFFFFFFFFFFFF" & _
"FFFF")
Global $ICON_TARGET_EMPTY = _WriteResource( _
"0x0000010001002020080000000000E80200001600000028000000200000004000000001000400000000000002" & _
"000000000000000000001000000010000000000000000000800000800000008080008000000080008000808000" & _
"00C0C0C000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF000000000000000000" & _
"00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FF" & _
"FFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"F000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFF" & _
"FFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFF" & _
"FFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF0" & _
"00FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000000007770CCCCCCCCCCCCCCCCCCCC" & _
"C07770007070CCCCCCCCCCCCCCCCCCCCC07070007770CCCCCCCCCCCCCCCCCCCCC0777000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000FFFFFFFF8000000080000000800000008000000080000000800000008000000080000000" & _
"800000008000000080000000800000008000000080000000800000008000000080000000800000008000000080" & _
"0000008000000080000000800000008000000080000000800000008000000080000000FFFFFFFFFFFFFFFFFFFF" & _
"FFFF")
; Loading cursor from file
$hTargetCursor = DllCall("User32.dll", "int", "LoadCursorFromFile", "str", $CURSOR_TARGET)
$hTargetCursor = $hTargetCursor[0]
; #FUNCTION# ====================================================================================================================
; Name...........: _FormCaptor_Create
; Description ...: Create a embeded Control Info Tool at the specified position
; Syntax.........: _FormCaptor_Create($iX, $iY)
; Parameters ....: $iX - The left side of the control
; $iY - The top of the control
; Return values .: Success: Returns the identifier (controlID) of the new control.
; Failure: Returns 0.
; ===============================================================================================================================
Func _FormCaptor_Create($iX, $iY)
$iFormCaptor = GUICtrlCreateIcon($ICON_TARGET_FULL, 0,$iX, $iY, 32, 32, BitOR($SS_NOTIFY,$WS_GROUP))
Return $iFormCaptor
EndFunc ;===>_FormCaptor_Create
Func _FormCaptor_Init()
GUIRegisterMsg ($WM_MOUSEMOVE, "WM_MOUSEMOVE_FUNC")
GUIRegisterMsg ($WM_LBUTTONUP, "WM_LBUTTONUP_FUNC")
_drawFrame()
EndFunc ;===>_FormCaptor_Init
Func _FormCaptor_SetCapture()
$FormCaptor_State = True
DllCall("user32.dll", "hwnd", "SetCapture", "hwnd", $hGUI)
$gOldCursor = DllCall("user32.dll", "int", "SetCursor", "int", $hTargetCursor)
If not @error Then $gOldCursor = $gOldCursor[0]
GUICtrlSetImage($iFormCaptor, $ICON_TARGET_EMPTY)
EndFunc ;===>_FormCaptor_SetCapture
Func _FormCaptor_Action()
If $iAction = 1 Then
$iAction = 0
Return 1
EndIf
Return 0
EndFunc ;===>_FormCaptor_Action
Func WM_MOUSEMOVE_FUNC($hWnd, $nMsg, $wParam, $lParam)
If not $FormCaptor_State Then Return 1
$hWindow = _GetHoveredHwndWindow()
If $hWindow <> $hOldWindow Then
$hOldWindow = $hWindow
EndIf
$winInfos = _WinInfos($hWindow)
$aControls = _listControls($hWindow)
$aHwndCtrlInfo = _GetHoveredHwndControl($aControls)
If IsArray($aHwndCtrlInfo) Then
_setLayer($hWindow, $aHwndCtrlInfo[13], $aHwndCtrlInfo[14], $aHwndCtrlInfo[9], $aHwndCtrlInfo[10])
Else
$aHwndCtrlInfo = $winInfos
_setLayer($hWindow, $winInfos[13], $winInfos[14], $winInfos[9], $winInfos[10])
EndIf
Return 1
EndFunc
Func WM_LBUTTONUP_FUNC($hWnd, $nMsg, $wParam, $lParam)
If not $FormCaptor_State Then Return 1
$FormCaptor_State = False
DllCall("user32.dll", "int", "ReleaseCapture")
DllCall("user32.dll", "int", "SetCursor", "int", $gOldCursor)
GUICtrlSetImage($hTargetPic, $ICON_TARGET_FULL)
ToolTip("")
_setLayer(0, -1, -1, -1, -1)
$iAction = 1
Return 1
EndFunc
;===============================================================================
; Write icons and cursor
;===============================================================================
Func _WriteResource($sbStringRes)
Local $sTempFile
Do
$sTempFile = @TempDir & "\temp" & Hex(Random(0, 65535), 4)
Until not FileExists($sTempFile)
Local $hFile = FileOpen($sTempFile, 2+16)
FileWrite($hFile, $sbStringRes)
FileClose($hFile)
Return $sTempFile
EndFunc
Func _listControls($hWindow)
$sList = WinGetClassList($hWindow)
$ctrls = StringSplit($sList, @LF)
$ctrls[0] = UBound($ctrls) - 1
_ArrayDelete($ctrls, $ctrls[0])
$ctrls[0] = UBound($ctrls) -1
If $ctrls[0] = 0 Then Return ""
_ArrayDelete($ctrls, 0)
_ArraySort($ctrls)
Local $controls[ UBound($ctrls) ][$cols]
$lastClass = ""
$n = 0
For $i = 0 To UBound($ctrls) - 1
If $ctrls[$i] <> $lastClass Then
$lastClass = $ctrls[$i]
$hInstance = 1
Else
$hInstance += 1
EndIf
$advClass = "[CLASS:" & $ctrls[$i] & "; INSTANCE:" & $hInstance & "]"
$hCtrl = ControlGetHandle($hWindow, "", $advClass)
$pos = ControlGetPos($hWindow, "", $advClass)
If NOT IsArray($pos) OR UBound($pos) <> 4 Then Local $pos[4] = [0, 0, 0, 0]
$hwndScreenPos = WinGetPos($hCtrl)
If NOT IsArray($hwndScreenPos) OR UBound($hwndScreenPos) <> 4 Then Local $hwndScreenPos[4] = [0, 0, 0, 0]
$controls[$i][0] = $hCtrl ; HANDLE
$controls[$i][1] = $ctrls[$i] ; CLASS
$controls[$i][2] = $hInstance ; Instance
$controls[$i][3] = $ctrls[$i] & $hInstance ; ClasseNameNN
$controls[$i][4] = $advClass ; Advanced Mode
$controls[$i][5] = _GetDlgCtrlID($hCtrl) ; ID
$controls[$i][6] = ControlGetText($hWindow, "", $advClass) ; Text
$controls[$i][7] = $pos[0] ; Position X
$controls[$i][8] = $pos[1] ; Position Y
$controls[$i][9] = $pos[2] ; Largeur
$controls[$i][10] = $pos[3] ; Hauteur
$controls[$i][11] = _GetWindowLong($hCtrl , $GWL_STYLE) ; Style
$controls[$i][12] = _GetWindowLong($hCtrl , $GWL_EXSTYLE) ; Style étendu
$controls[$i][13] = $hwndScreenPos[0] ; Position X (par rapport à l'écran)
$controls[$i][14] = $hwndScreenPos[1] ; Position Y (par rapport à l'écran)
Next
_ArraySort($controls, 0, 0, 0, 5)
Return $controls
EndFunc ;===>_listControls
Func _setLayer($hOwner, $x, $y, $w, $h)
If NOT $showLayer Then Return 1
For $i = 0 To UBound($frame) - 1
; GUISetState( @SW_HIDE, $frame[$i] )
WinSetTrans ($frame[$i], "", 0)
Next
If $x = -1 AND $y = -1 AND $w = -1 AND $h = -1 Then Return 0
WinMove($frame[0], "", $x, $y, $w, $lineSize )
WinMove($frame[1], "", $x + $w - $lineSize, $y, $lineSize, $h )
WinMove($frame[2], "", $x, $y, $lineSize, $h )
WinMove($frame[3], "", $x, $y + $h - $lineSize, $w, $lineSize )
For $i = 0 To UBound($frame) - 1
WinSetTrans ($frame[$i], "", 255)
GUISetState(@SW_DISABLE, $frame[$i])
_SetWindowPos($frame[$i], _GetWindow($hOwner, $GW_HWNDPREV), 0, 0, 0, 0, BitOR($SWP_DRAWFRAME , $SWP_NOACTIVATE, $SWP_NOMOVE, $SWP_NOSIZE))
Next
EndFunc
Func _drawFrame()
$frame[0] = GUICreate("-", 100, $lineSize, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW )
$frame[1] = GUICreate("-", $lineSize, 100, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW )
$frame[2] = GUICreate("-", 100, $lineSize, 0, 100, $WS_POPUP, $WS_EX_TOOLWINDOW )
$frame[3] = GUICreate("-", $lineSize, 100, 100, 0, $WS_POPUP, $WS_EX_TOOLWINDOW )
For $i = 0 To UBound($frame) - 1
If $i > 0 Then $hOwner = $frame[$i - 1]
GUISetBkColor ($layerColor, $frame[$i])
GUISetState(@SW_SHOW, $frame[$i])
WinSetTrans ($frame[$i], "", 0)
Next
EndFunc ;===>_DrawFrame
Func _WinInfos($hWindow)
Local $infos[15]
$wPos = WinGetPos($hWindow)
If NOT IsArray($wPos) Then Local $wPos[4] = [0, 0, 0, 0]
$infos[0] = $hWindow ; HANDLE
$infos[1] = _GetWindowClass($hWindow) ; CLASS
$infos[2] = "" ; Instance
$infos[3] = "" ; ClasseNameNN
$infos[4] = "" ; Advanced Mode
$infos[5] = "" ; ID
$infos[6] = WinGetText($hWindow) ; Text
$infos[7] = $wPos[0] ; Position X
$infos[8] = $wPos[1] ; Position Y
$infos[9] = $wPos[2] ; Largeur
$infos[10] = $wPos[3] ; Hauteur
$infos[11] = _GetWindowLong($hWindow, $GWL_STYLE) ; Style
$infos[12] = _GetWindowLong($hWindow, $GWL_EXSTYLE) ; Style étendu
$infos[13] = $wPos[0] ; Position X (par rapport à l'écran)
$infos[14] = $wPos[1] ; Position Y (par rapport à l'écran)
Return $infos
EndFunc
Func _GetHoveredHwndWindow()
$i_xpos = MouseGetPos(0)
$i_ypos = MouseGetPos(1)
Local $iRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", $i_xpos, "long", $i_ypos)
If IsArray($iRet) Then
$appHandle = $iRet[0]
Local Const $hParent = _GetAncestor($appHandle, $GA_ROOT)
If $hParent = $hGUI Then Return 0
Return $hParent
Else
Return SetError(1, 0, 0)
EndIf
EndFunc ;==>GetHoveredHwnd
Func _GetHoveredHwndControl($aControls)
Local $activeCtrlIndex = -1
Local $mousePos = MouseGetPos()
Local $hoverCtrl[1][2]
Local $n = 0
Local $result[$cols]
For $i = 0 To UBound($aControls) - 1
$ctrlPosX1 = $aControls[$i][13]
$ctrlPosX2 = $aControls[$i][13] + $aControls[$i][9]
$ctrlPosY1 = $aControls[$i][14]
$ctrlPosY2 = $aControls[$i][14] + $aControls[$i][10]
If $mousePos[0] > $ctrlPosX1 AND $mousePos[0] < $ctrlPosX2 AND $mousePos[1] > $ctrlPosY1 AND $mousePos[1] < $ctrlPosY2 AND BitAND(WinGetState ($aControls[$i][0]), 2) Then
$diffX1 = $mousePos[0] - $ctrlPosX1
$diffX2 = $ctrlPosX2 - $mousePos[0]
$diffY1 = $ctrlPosY1 - $mousePos[1]
$diffY2 = $ctrlPosY2 - $mousePos[1]
$diff = $diffX1 + $diffX2 + $diffY1 + $diffY2
$n += 1
Redim $hoverCtrl[$n][2]
$hoverCtrl[$n - 1][0] = $i
$hoverCtrl[$n - 1][1] = $diff
EndIf
Next
_ArraySort($hoverCtrl, 0, 0, 0, 1)
$hoverCtrlIndex = $hoverCtrl[0][0]
If $hoverCtrlIndex = "" Then Return 0
For $i = 0 To $cols - 1
$result[$i] = $aControls[$hoverCtrlIndex][$i]
Next
Return $result
EndFunc ;===>_GetHoveredHwndControl
; #Fonctions from WinAPI.au3 ====================================================================================================
Func _GetWindowClass($hWnd)
$pClassName = DllStructCreate("char[256]")
DllCall("user32.dll", "int", "GetClassName", "hwnd", $hWnd, "ptr", DllStructGetPtr($pClassName), "int", 255)
Return DllStructGetData($pClassName, 1)
EndFunc ;====>_GetWindowClass
Func _GetWindowLong($hWnd, $nParam)
Local $ret = DllCall("user32", "int", "GetWindowLong", "hWnd", $hWnd, "int", $nParam)
If not @error Then Return $ret[0]
EndFunc ;===>_GetWindowLong
Func _GetDlgCtrlID($hWnd)
Local $aResult = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hWnd)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0]
EndFunc ;==>_GetDlgCtrlID
Func _GetAncestor($hWnd, $iFlags = 1)
Local $aResult = DllCall("user32.dll", "hwnd", "GetAncestor", "hwnd", $hWnd, "uint", $iFlags)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0]
EndFunc ;==>_GetAncestor
Func _GetWindow($hWnd, $iCmd)
Local $aResult = DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $hWnd, "uint", $iCmd)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0]
EndFunc ;==>_GetWindow
Func _SetWindowPos($hWnd, $hAfter, $iX, $iY, $iCX, $iCY, $iFlags)
Local $aResult = DllCall("user32.dll", "bool", "SetWindowPos", "hwnd", $hWnd, "hwnd", $hAfter, "int", $iX, "int", $iY, "int", $iCX, _
"int", $iCY, "uint", $iFlags)
If @error Then Return SetError(@error, @extended, False)
Return $aResult[0]
EndFunc ;==>_SetWindowPos
;===============================================================================
; OnExit handler
;===============================================================================
Func OnAutoitExit()
; Standard @exitcode doesn't work...
If IsDeclared("CURSOR_TARGET") Then
FileDelete($ICON_TARGET_FULL)
FileDelete($ICON_TARGET_EMPTY)
FileDelete($CURSOR_TARGET)
EndIf
EndFunc