; Example script, showing the usage of COM Event functions.
;
; See also: http://msdn.microsoft.com/en-us/library/aa768400.aspx

; We use a very simple GUI to show the results of our Events.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Global $g_idGUIEdit, $g_idGUIProg

Example()
Exit ; End of our Demo.

Func Example()
    Local $hGUIMain = GUICreate("Event Test", 600, 500)
    $g_idGUIEdit = GUICtrlCreateEdit("Test Log:" & @CRLF, 10, 20, 580, 400)
    $g_idGUIProg = GUICtrlCreateProgress(10, 5, 580, 10)
    Local $idGUIExit = GUICtrlCreateButton(" Close ", 250, 450, 80, 30)
    GUISetState() ;Show GUI

    ; We prepare the Internet Explorer as our test subject
    Local $oIE = ObjCreate("InternetExplorer.Application.1")
    With $oIE
        .Visible = 1
        .Top = (@DesktopHeight - 400) / 2
        .Height = 400 ; Make it a bit smaller than our GUI.
        .Width = 600
        .Silent = 1 ; Don't show IE's dialog boxes
        Local $hIEWnd = HWnd(.hWnd) ; Remember the Window, in case user decides to close it
    EndWith

    ; We choose for a specific Internet Explorer interface 'DWebBrowserEvents' because the IE is subject
    ; to modifications by e.g. Visual Studio and Adobe Acrobat Reader. If you have IE-plugins installed,
    ; AutoIt might not be able to find the correct interface automatically.
    Local $oEventObject = ObjEvent($oIE, "IEEvent_", "DWebBrowserEvents")
    If @error Then
        MsgBox($MB_OK, "AutoIt COM Test", _
                "ObjEvent: Can't use event interface 'DWebBrowserEvents'. Error code: " & Hex(@error, 8))
        Exit
    EndIf

    ; Now starting to load an example Web page.
    Local $sURL = "http://www.AutoItScript.com/"
    $oIE.Navigate($sURL)
    Sleep(1000) ; Give it some time to load the web page

    GUISwitch($hGUIMain) ; Switch back to our GUI in case IE stole the focus

    ; Waiting for user to close the GUI.
    Local $iMsg
    While 1
        $iMsg = GUIGetMsg()
        If $iMsg = $GUI_EVENT_CLOSE Or $iMsg = $idGUIExit Then ExitLoop
    WEnd

    $oEventObject.Stop ; Tell IE we don't want to receive events.
    $oEventObject = 0 ; Kill the Event Object
    If WinExists($hIEWnd) Then $oIE.Quit ; Close IE Window
    $oIE = 0 ; Remove IE from memory (not really necessary).

    GUIDelete() ; Remove GUI
EndFunc   ;==>Example

; A few Internet Explorer Event Functions
; See also: http://msdn.microsoft.com/en-us/library/aa768400.aspx
Func IEEvent_BeforeNavigate($sURL, $iFlags, $sTargetFrameName, $dPostData, $sHeaders, $bCancel)
    ;   Note: the declaration is different from the one on MSDN.
    GUICtrlSetData($g_idGUIEdit, "BeforeNavigate: " & $sURL & " Flags: " & $iFlags & " tgframe: " & $sTargetFrameName & " Postdat: " & $dPostData & " Hdrs: " & $sHeaders & " canc: " & $bCancel & @CRLF, "append")
EndFunc   ;==>IEEvent_BeforeNavigate

Func IEEvent_ProgressChange($iProgress, $iProgressMax)
    If $iProgressMax > 0 Then
        GUICtrlSetData($g_idGUIProg, ($iProgress * 100) / $iProgressMax)
    EndIf
EndFunc   ;==>IEEvent_ProgressChange

Func IEEvent_StatusTextChange($sText)
    GUICtrlSetData($g_idGUIEdit, "IE Status text changed to: " & $sText & @CRLF, "append")
EndFunc   ;==>IEEvent_StatusTextChange

Func IEEvent_PropertyChange($szProperty)
    GUICtrlSetData($g_idGUIEdit, "IE Changed the value of the property: " & $szProperty & @CRLF, "append")
EndFunc   ;==>IEEvent_PropertyChange

Func IEEvent_DownloadComplete()
    GUICtrlSetData($g_idGUIEdit, "IE has finished a navigation operation" & @CRLF, "append")
EndFunc   ;==>IEEvent_DownloadComplete

Func IEEvent_NavigateComplete($sURL)
    ;   Note: the declaration is different from the one on MSDN.
    GUICtrlSetData($g_idGUIEdit, "IE has finished loading URL: " & $sURL & @CRLF, "append")
EndFunc   ;==>IEEvent_NavigateComplete

Func IEEvent_($sEventName)
    ; This is an optional event function to catch non-defined events.
    ; The parameter contains the name of the event being called.
    GUICtrlSetData($g_idGUIEdit, "Uncatched event: " & $sEventName & @CRLF, "append")
EndFunc   ;==>IEEvent_