[EX] Intercepter une MsgBox et extraire son contenu.

Partagez vos scripts, et vos applications AutoIt.
Règles du forum
.
Répondre
Avatar du membre
Numeric
Niveau 5
Niveau 5
Messages : 131
Enregistré le : mer. 23 mars 2016 08:17
Status : Hors ligne

[EX] Intercepter une MsgBox et extraire son contenu.

#1

Message par Numeric »

Sur le forum, un utilisateur avait exprimé le besoin d'intercepter automatiquement l'apparition des boîtes de dialogue MsgBox afin d'extraire leur contenu, notamment les titres et les textes associés. L'objectif était de permettre à un système d'assistance vocale de lire ces informations à l'utilisateur. Cependant, je ne retrouve plus le fil de discussion correspondant dans les archives du forum.

En réponse à cette demande, je vous propose ci-dessous un script permettant d'intercepter et d'extraire les informations des MsgBox de manière automatisée. Ce script peut être utilisé dans divers contextes, notamment pour l'accessibilité des personnes utilisant des systèmes d'assistance vocale ou pour automatiser certaines tâches basées sur des boîtes de dialogue MsgBox :

#include <WinAPI.au3>


HotKeySet("{Esc}", "Quitter")

Global Const $HCBT_ACTIVATE = 5


Global $hCBT_Proc = DllCallbackRegister("_WH_CBT", "int", "int;int;int")
If Not $hCBT_Proc Then
   MsgBox(16, "Error!", "impossible d'enregistrer la fonction de rappel")
   Exit
EndIf

Global $hCBT_Hook = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hCBT_Proc), 0, _WinAPI_GetCurrentThreadId())
If Not $hCBT_Hook Then
   DllCallbackFree($hCBT_Proc)
   MsgBox(16, "Erreur", "impossible d'installer le hook")
   Exit
EndIf

;On affiche cette MsgBox pour l'exemple.
MsgBox(64, "Titre: un message", "Contenu: ceci est un message")


While 1
   Sleep(100)
WEnd

Func _WH_CBT($nCode, $wParam, $lParam)
   If $nCode < 0 Then
      Return _WinAPI_CallNextHookEx($hCBT_Hook, $nCode, $wParam, $lParam)
   EndIf

   Switch $nCode
      Case $HCBT_ACTIVATE
         Local $hWnd = HWnd($wParam)
         Local $hLabel = FindMsgBoxTextDisplayHwnd($hWnd)

         Local $sText = _WinAPI_GetWindowText($hLabel)
         TrayTip("Title: " & WinGetTitle($hWnd), "Text : " & $sText, 20, 1)
   EndSwitch

   Return _WinAPI_CallNextHookEx($hCBT_Hook, $nCode, $wParam, $lParam)
EndFunc   ;==>_WH_CBT


; #FUNCTION# ====================================================================================================================
; Name ..........: FindMsgBoxTextDisplayHwnd
; Description ...: Finds the handle of the text display area in a message box window.
; Syntax ........: FindMsgBoxTextDisplayHwnd($hWnd)
; Parameters ....: $hWnd                - A handle value representing the message box window.
; Return values .: On success, returns the handle of the text display area (control). On failure, returns 0 and sets @error:
;                   1 - If the specified window handle does not belong to a message box window.
;                   2 - If an error occurs during the enumeration of child windows.
; Author ........: Numeric
; Modified ......:
; Remarks .......: This function is useful for extracting the text content from a message box. Since message boxes are blocking,
;                  this function should be called after displaying the message box to ensure the window handle passed as an argument
;                  represents the active message box.
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func FindMsgBoxTextDisplayHwnd($hWnd)
    If Not _WinAPI_IsClassName($hWnd, "#32770") Then Return SetError(1, 0, 0)
    Local $hModalControl = 0

    Local $aChild = _WinAPI_EnumChildWindows($hWnd, False)
    If @error Then Return SetError(2, 0, 0)

    For $i = 1 To UBound($aChild, 2)
        If StringCompare($aChild[$i][1], "Static") = 0 Then
            $hModalControl = $aChild[$i][0]
            ExitLoop
        EndIf
    Next
    Local $hControl = ControlGetHandle($hWnd, _WinAPI_GetWindowText($hWnd), "[CLASS:Static; INSTANCE:2]")
    $hControl = $hControl = 0 ? $hModalControl : $hControl
    Return $hControl
EndFunc   ;==>FindMsgBoxTextDisplayHwnd

Func Quitter()
   _WinAPI_UnhookWindowsHookEx($hCBT_Hook)
   Exit
EndFunc   ;==>Quitter
 
De 0 et 1 vers les étoiles , tout part du Binaire, Numeric
Répondre