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
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