函数参考


ObjEvent

包含一个已经得到的Object(对象)的事件句柄.

ObjEvent ( $ObjectVar, "functionprefix" [, "接口名称"] )
ObjEvent ( "AutoIt.Error" [, "函数名称"] )

参数

$ObjectVar A variable containing an Object from which you want to receive events
"functionprefix" The prefix of the functions you define to handle receiving events.
The prefix is appended by the Objects method name.
"接口名称" [可选参数] name of an Event interface to use.
Note: It must be a supported as outgoing for the
Object AND it must be of type DISPATCH.

返回值

成功: 返回 object (对象) 或者 函数名称.
失败: 返回 空字符串 "" 并设置 @error 为 1.

注意/说明

The first format is used to receive Events from the given Object.
To receive a specific event, create an AutoIt function name using
the given prefix appended with the event name.

The second format is used for COM Error Handling. If any COM error
occurs, the given function is being called. First parameter for the
function will be error object. You can use it to access different properties
of this object.
If the second parameter is omitted, it will return the name of the
current Error handler function, if present.

参考 Obj/COM 参考 相关说明.

相关

ObjGet, IsObj, ObjCreate, GUICtrlCreateObj

示例/演示


_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(48 + 262144, "COM Error", "@error is set to COM error number." & @CRLF & "@error = " & @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($IEpDisp, $IEURL, $IEFlags, $IETargetFrameName, $IEPostData, $IEHeaders, $IECancel)
    ConsoleWrite("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--BeforeNavigate2 fired--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & _
            "$IEpDisp = " & $IEpDisp() & "  -  " & ObjName($IEpDisp) & @CRLF & _ ; e.g. default property and name for the object
            "$IEURL = " & $IEURL & @CRLF & _
            "$IEFlags = " & $IEFlags & @CRLF & _
            "$IETargetFrameName = " & $IETargetFrameName & @CRLF & _
            "$IEPostData = " & $IEPostData & @CRLF & _
            "$IEHeaders = " & $IEHeaders & @CRLF & _
            "$IECancel = " & $IECancel & @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("err.number is: " & @TAB & $oError.number & @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc