对一定范围的事件设置事件钩子程序
#Include <WinAPIEx.au3>
_WinAPI_SetWinEventHook( $iEventMin, $iEventMax, $pWinEventProc [, $iProcessID [, $iThreadID [, $iFlags]]] )
$iEventMin | 由钩子程序控制的事件($EVENT_*)范围的最低值 |
$iEventMax | 由钩子程序控制的事件($EVENT_*)范围的最高值 |
$pWinEventProc | 在响应可访问对象创建的事件中系统调用的应用程序定义的钩子程序的地址. |
$iProcessID | [可选参数] 钩子函数接受事件的进程的ID. 如果该参数为0, 钩子函数与当前桌面上的所有进程相关联. |
$iThreadID | [可选参数] 钩子函数接受事件的线程的ID. 如果该参数为0, 钩子函数与当前桌面上的所有线程相关联. |
$iFlags | [可选参数] 指定钩子函数及忽略的事件的位置的标记. 可用下列值或值的组合: $WINEVENT_INCONTEXT $WINEVENT_OUTOFCONTEXT $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 |
成功: | 返回标识事件钩子实例的值 |
失败: | 返回 0并设置@error非0 |
在MSDN中搜索
#Include <APIConstants.au3>
#Include <GUIMenu.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $hEventProc = DllCallbackRegister('_EventProc', 'none', 'ptr;dword;hwnd;long;long;dword;dword')
Global $tPOINT, $tRECT, $hEventHook, $Index, $hMenu = 0
OnAutoItExitRegister('OnAutoItExit')
$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)
Switch $iEvent
Case $EVENT_SYSTEM_MENUPOPUPSTART
; 添加 "查看 - 计算器"
$hMenu = _SendMessage($hWnd, $MN_GETHMENU)
If (_GUICtrlMenu_IsMenu($hMenu)) And (StringInStr(_GUICtrlMenu_GetItemText($hMenu, 0), 'Status Bar')) And (StringInStr(_WinAPI_GetWindowFileName($hWnd), 'notepad.exe')) Then
$Index = _GUICtrlMenu_GetItemCount($hMenu)
_GUICtrlMenu_InsertMenuItem($hMenu, $Index, 'Calculator' & @TAB & ':-)')
$tRECT = _GUICtrlMenu_GetItemRectEx($hWnd, $hMenu, $Index)
Else
$hMenu = 0
EndIf
Case $EVENT_SYSTEM_MENUPOPUPEND
If $hMenu Then
_GUICtrlMenu_DeleteMenu($hMenu, $Index)
$tPOINT = _WinAPI_GetMousePos()
If _WinAPI_PtInRect($tRECT, $tPOINT) Then
Run(@SystemDir & '\calc.exe')
EndIf
$hMenu = 0
EndIf
EndSwitch
EndFunc ;==>_EventProc