ObjEvent
处理特定对象的传入事件.
ObjEvent ( $ObjectVar, "functionprefix" [, "interface name"] )
ObjEvent ( "AutoIt.Error" [, "function"] )
参数
$ObjectVar
|
接收事件的对象变量名称
|
"functionprefix"
|
处理接收事件的函数前缀.
前缀是追加对象的方法名.
|
"interface name"
|
[可选] 事件的接口名称.
注意:它必须支持即将传出的对象, 而且必须是发送类型
Object AND 必须为 DISPATCH 类型.
|
返回值
成功:
|
返回对象或函数的名称.
|
失败:
|
设置 @error 值.
|
备注
第一个语句格式用来接收特定的对象事件.
要接收的特定事件, 创建一个使用给定前缀附加事件名称命名的 AutoIt 函数.
第二种格式用于 COM 的错误处理
如果任何 COM 错误发生时, 调用指定的函数. 函数的第一个参数是错误的对象.
您可以使用它来访问这个对象的不同属性.
如果省略第二个参数, 则返回注册时使用的当前错误处理函数的名称.
AutoIt 错误对象的属性
.number
|
COM 调用的 Windows HRESULT 值
|
.windescription
|
来自 .number 的 FormatWinError() 文本
|
.source
|
生成错误的对象的名称 (内容来自 ExcepInfo.source)
|
.description
|
错误的源对象描述 (内容来自 ExcepInfo.description)
|
.helpfile
|
错误的源对象帮助文件 (内容来自 ExcepInfo.helpfile)
|
.helpcontext
|
错误的源对象帮助文件环境 ID (内容来自 ExcepInfo.helpcontext)
|
.lastdllerror
|
GetLastError() 的返回数
|
.scriptline
|
生成错误的脚本行
|
见 Obj/COM 参考 的相关说明.
相关
GUICtrlCreateObj, IsObj, ObjCreate, ObjGet
函数示例
示例 1
#include <MsgBoxConstants.au3>
Example()
Func Example()
; Error monitoring. This will trap all COM errors while alive.
; This particular object is declared as local, meaning after the function returns it will not exist.
Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
; Create Internet Explorer object
Local $oIE = ObjCreate("InternetExplorer.Application")
; Check for errors
If @error Then Return
$oIE.Visible = True ; set visibility
; Custom sink object
Local $oIEEvents = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2")
; Navigate somewhere
$oIE.navigate("http://www.google.com/")
; Check for errors while loading
If @error Then
$oIE.Quit()
Return
EndIf
; Wait for page to load
While 1
If $oIE.readyState = "complete" Or $oIE.readyState = 4 Then ExitLoop
Sleep(10)
WEnd
; Deliberately cause error by calling non-existing method
$oIE.PlayMeARockAndRollSong()
; Check for errors
If @error Then MsgBox($MB_SYSTEMMODAL, "COM Error", "@error is set to COM error number." & @CRLF & "@error = 0x" & Hex(@error))
; Wait few seconds to see if more events will be fired
Sleep(3000)
; Nothing more to do. Close IE and return from the function
$oIE.Quit()
#forceref $oErrorHandler, $oIEEvents
EndFunc ;==>Example
; BeforeNavigate2 method definition
Func _IEEvent_BeforeNavigate2($oIEpDisp, $sIEURL, $iIEFlags, $sIETargetFrameName, $sIEPostData, $iIEHeaders, $bIECancel)
ConsoleWrite("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--BeforeNavigate2 fired--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & _
"$oIEpDisp = " & $oIEpDisp() & " - " & ObjName($oIEpDisp) & @CRLF & _ ; e.g. default property and name for the object
"$sIEURL = " & $sIEURL & @CRLF & _
"$iIEFlags = " & $iIEFlags & @CRLF & _
"$sIETargetFrameName = " & $sIETargetFrameName & @CRLF & _
"$sIEPostData = " & $sIEPostData & @CRLF & _
"$iIEHeaders = " & $iIEHeaders & @CRLF & _
"$bIECancel = " & $bIECancel & @CRLF & _
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & @CRLF)
EndFunc ;==>_IEEvent_BeforeNavigate2
; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
; Do anything here.
ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc ;==>_ErrFunc
示例 2
Global $__g_oTemplateCOMErrorHandler = 0
Local $oShell = ObjCreate("Shell.Application")
; Wrap calls that are likely to fatal error
; This won't fatal error, it will set @error as in 3.3.11.0
Template_COMErrorRegister()
$oShell.InvalidFunction()
If @error Then MsgBox(4096, "COM Error Detected", @error)
Template_COMErrorUnregister()
; This will fatal error, the 2nd line will not be reached.
$oShell.InvalidFunction()
If @error Then MsgBox(4096, "COM Error Detected", @error)
Func Template_COMErrorRegister()
$__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc ;==>Template_COMErrorRegister
Func Template_COMErrorUnregister()
$__g_oTemplateCOMErrorHandler = 0
EndFunc ;==>Template_COMErrorUnregister
Func Template_COMErrFunc()
; Do nothing special, just check @error after suspect functions.
EndFunc ;==>Template_COMErrFunc
----------------------------------------
|