Function Reference


_WinAPI_SetWinEventHook

Sets an event hook function for a range of events

#include <WinAPISys.au3>
_WinAPI_SetWinEventHook ( $iEventMin, $iEventMax, $pEventProc [, $iPID = 0 [, $iThreadId = 0 [, $iFlags = 0]]] )

Parameters

$iEventMin The lowest event value in the range of events ($EVENT_*) that are handled by the hook function.
$iEventMax The highest event value in the range of events ($EVENT_*) that are handled by the hook function.
$pEventProc The address of an application-defined hook function that the system calls in response to
events generated by an accessible object.
$iPID [optional] The ID of the process from which the hook function receives events. If this parameter is 0 (Default),
the hook function is associated with all existing processes on the current desktop.
$iThreadId [optional] The ID of the thread from which the hook function receives events. If this parameter is 0 (Default),
the hook function is associated with all existing threads on the current desktop.
$iFlags [optional] The flags that specify the location of the hook function and of the events to be skipped.
The following flags are valid:
    $WINEVENT_INCONTEXT
    $WINEVENT_OUTOFCONTEXT (Default)
    $WINEVENT_SKIPOWNPROCESS
    $WINEVENT_SKIPOWNTHREAD

The following single flags, or flag combinations are valid:
    $WINEVENT_INCONTEXT
    $WINEVENT_OUTOFCONTEXT
    $WINEVENT_INCONTEXT | $WINEVENT_SKIPOWNPROCESS
    $WINEVENT_INCONTEXT | $WINEVENT_SKIPOWNTHREAD
    $WINEVENT_OUTOFCONTEXT | $WINEVENT_SKIPOWNPROCESS
    $WINEVENT_OUTOFCONTEXT | $WINEVENT_SKIPOWNTHREAD

Return Value

Success: A value that identifies this event hook instance.
Failure: 0.

Remarks

Clients can call _WinAPI_SetWinEventHook() multiple times if they want to register additional hook functions
or listen for additional events.

See Also

Search SetWinEventHook in MSDN Library.

Example

#include <APISysConstants.au3>
#include <GuiMenu.au3>
#include <SendMessage.au3>
#include <WinAPIGdi.au3>
#include <WinAPIMisc.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Local $hEventProc = DllCallbackRegister('_EventProc', 'none', 'ptr;dword;hwnd;long;long;dword;dword')
Global $g_tRECT, $g_iIndex, $g_hMenu = 0

OnAutoItExitRegister('OnAutoItExit')

Local $hEventHook = _WinAPI_SetWinEventHook($EVENT_SYSTEM_MENUPOPUPSTART, $EVENT_SYSTEM_MENUPOPUPEND, DllCallbackGetPtr($hEventProc))

Run(@SystemDir & '\notepad.exe')

While 1
    Sleep(1000)
WEnd

Func OnAutoItExit()
    _WinAPI_UnhookWinEvent($hEventHook)
    DllCallbackFree($hEventProc)
EndFunc   ;==>OnAutoItExit

Func _EventProc($hEventHook, $iEvent, $hWnd, $iObjectID, $iChildID, $iThreadId, $iEventTime)
    #forceref $hEventHook, $iObjectID, $iChildID, $iThreadId, $iEventTime

    Switch $iEvent
        Case $EVENT_SYSTEM_MENUPOPUPSTART
            ; Add "View - Calculator"
            $g_hMenu = _SendMessage($hWnd, $MN_GETHMENU)
            If (_GUICtrlMenu_IsMenu($g_hMenu)) And (StringInStr(_GUICtrlMenu_GetItemText($g_hMenu, 0), 'Status Bar')) And (StringInStr(_WinAPI_GetWindowFileName($hWnd), 'notepad.exe')) Then
                $g_iIndex = _GUICtrlMenu_GetItemCount($g_hMenu)
                _GUICtrlMenu_InsertMenuItem($g_hMenu, $g_iIndex, 'Calculator' & @TAB & ':-)')
                $g_tRECT = _GUICtrlMenu_GetItemRectEx($hWnd, $g_hMenu, $g_iIndex)
            Else
                $g_hMenu = 0
            EndIf
        Case $EVENT_SYSTEM_MENUPOPUPEND
            If $g_hMenu Then
                _GUICtrlMenu_DeleteMenu($g_hMenu, $g_iIndex)
                Local $tPOINT = _WinAPI_GetMousePos()
                If _WinAPI_PtInRect($g_tRECT, $tPOINT) Then
                    Run(@SystemDir & '\calc.exe')
                EndIf
                $g_hMenu = 0
            EndIf
    EndSwitch
EndFunc   ;==>_EventProc