Jump to content

Multiple usage of 'HTMLDocumentEvents2'?


 Share

Recommended Posts

Greetings,

  In the code sample below I created a GUI with 2 "TabItem" Controls.  In each tab an object is created and a website is loaded and embedded within.  The 'first' tab loads a site from MS and the 'second' loads the "Google.com" site.

  The 'first' tab is setup to handle 'events' like registering 'clicks' and 'double-clicks' and 'mouse movements'...I would like to do the same with the 'second' tabitem (the tab holding the "Google.com") however it seems if I try to do that then the 'HTMLDocumentEvents2' causes interference with the website events in the 'first' tabitem.  Does anybody know a way to handle the document events separately so I can handle 'clicks' and 'movements' independently in each embedded website...? 

  In other words if I 'uncomment' the lines in the 'second' tabitem and the 'IEEvent3_onClick' function it seems to make the event handling problematic between the two windows...is there a way around this...?  Thanks in advance for any suggestions.

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

AutoItSetOption("GUIOnEventMode", 1)

 Global $oIE, $oIE2, $g_idGUIEdit, $hIE, $hIE_TR, $oDocument, $oDocument_TR

 Global $MainGUI = GUICreate("Utility Tool", 1024, 689)
 GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
 GUISetState(@SW_SHOW)

     $_THE_TABS = GUICtrlCreateTab(1, 40, 1024, 612, BitOR($GUI_SS_DEFAULT_TAB, $TCS_FOCUSONBUTTONDOWN))

 $_Scrape_TAB = GUICtrlCreateTabItem("Data Scrape")

    $g_idGUIEdit = GUICtrlCreateEdit("", 5, 465, 990, 175)
    GUICtrlSetBkColor(-1, 0x000000)
    GUICtrlSetColor(-1, 0x00FF00)
    GUICtrlSetFont(-1, 9, 400, -1, 'Courier New')
    GUICtrlCreateLabel("Below are some Browser events 'captured' from the above web page by AutoIt", 5, 450, 990, 20)


    ; We prepare the Internet Explorer as our test subject
    Global $oIE = ObjCreate("Shell.Explorer.2")
    $hIE = GUICtrlCreateObj($oIE, 10, 110, 1008, 330) ; <- insert $oIE in the AutoIt GUI

    ; Here we load an example Web page just to have something viewed in the browser
    $oIE.navigate('http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onmousemoveEX.htm')

    Sleep(1000) ; Give it some time to load the web page

    Do ; wait for document
        Sleep(250)
        $oDocument = $oIE.document
    Until IsObj($oDocument)

    ;   +  Scripting Object Interfaces
    ;   |  ---------------------------
    ;   |  https://msdn.microsoft.com/en-us/library/hh801967(v=vs.85).aspx
    ;   |
    ;   +-->   HTMLDocumentEvents2 interface (catch OnClick, OnMouseOver, .... etc
    ;          -----------------------------
    ;          https://msdn.microsoft.com/en-us/library/aa769764(v=vs.85).aspx
    ;
    Global $oEventObject = ObjEvent($oDocument, "IEEvent2_", "HTMLDocumentEvents2")

    If @error Then
        MsgBox($MB_OK, "AutoIt COM Test", _
                "ObjEvent: Can't use event interface 'HTMLDocumentEvents2'. Error code: " & Hex(@error, 8))
        Exit
    EndIf

$_Process_TAB = GUICtrlCreateTabItem("Processes")

 Sleep(2000)
               ; We prepare the Internet Explorer as our test subject
               Global $oIE2 = ObjCreate("Shell.Explorer.2")
               $hIE_TR = GUICtrlCreateObj($oIE2, 10, 110, 1008, 330) ; <- insert $oIE2 in the AutoIt GUI


               $oIE2.navigate("https://www.google.com")

               Sleep(1000) ; Give it some time to load the web page

                Do ; wait for document
                Sleep(250)
                $oDocument_TR = $oIE2.document
                Until IsObj($oDocument_TR)

;               Global $oEventObject_TR = ObjEvent($oDocument_TR, "IEEvent3_", "HTMLDocumentEvents2")
;
;                if @error Then
;                MsgBox($MB_OK, "AutoIt COM Test", _
;                       "ObjEvent: Can't use event interface 'HTMLDocumentEvents2'. Error code: " & Hex(@error, 8))
;                Exit
;                EndIf



 GUICtrlCreateTabItem("")  ;end TAB ITEMS definition...


 GUISwitch($MainGUI)

 Main()

Func Main()

    While 1

        Sleep(10)


    WEnd
EndFunc


;////////////////////////////////////////////////
;////////////////////////////////////////////////

; A few Internet Explorer Event Functions
; ( reference to the Event Obj interface: )
; ( https://msdn.microsoft.com/en-us/library/aa703876(v=vs.85).aspx )
;
Volatile Func IEEvent2_onClick($oEvent)
    ConsolePrint("mouse click: " & $oEvent.clientX & ',' & $oEvent.clientY & '  on ' & $oEvent.srcElement.NodeName)
    ConsolePrint("click text: " & $oEvent.srcElement.innertext)

EndFunc   ;==>IEEvent2_onClick

Volatile Func IEEvent2_onDblClick($oEvent)
    ConsolePrint("mouse DoubleClick: @" & $oEvent.clientX & ',' & $oEvent.clientY)

EndFunc   ;==>IEEvent2_onDblClick

Volatile Func IEEvent2_onMouseMove($oEvent)
    ConsolePrint("mouse moved to:" & @TAB & "Xpos = " & $oEvent.clientX & @TAB & "Ypos = " & $oEvent.clientY)
EndFunc   ;==>IEEvent2_onMouseMove

;Volatile Func IEEvent3_onClick($oEvent2)
;    ConsolePrint("mouse click: " & $oEvent2.clientX & ',' & $oEvent2.clientY & '  on ' & $oEvent2.srcElement.NodeName)
;    ConsolePrint("mouse click: " & $oEvent2.srcElement.innertext)
;EndFunc   ;==>IEEvent3_onClick

Func ConsolePrint($sMsg)
    Local Const $iMaxLines = 9 ; keep last 9 log lines only
    $sMsg = @HOUR & ':' & @MIN & ':' & @SEC & ':' & @MSEC & @TAB & $sMsg & @CRLF
    $sMsg = StringReplace(GUICtrlRead($g_idGUIEdit) & $sMsg, @CR, @CR)
    If @extended > $iMaxLines Then ; more than $iMaxLines
        $sMsg = StringMid($sMsg, StringInStr($sMsg, @CR, 0, -1 * $iMaxLines) + 2)
    EndIf
    GUICtrlSetData($g_idGUIEdit, $sMsg)
EndFunc   ;==>ConsolePrint

;*****

 Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            FileClose($_file)

    $oEventObject.Stop ; Tell IE we don't want to receive events.
    $oEventObject = 0 ; Kill the Event Object
    ;$oEventObject_TR.Stop ; Tell IE we don't want to receive events.
    ;$oEventObject_TR = 0 ; Kill the Event Object
    $oIE2 = 0 ; Remove IE from memory (not really necessary).

    GUIDelete() ; Remove GUI

            Exit

        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

    EndSelect
 EndFunc   ;==>SpecialEvents

   

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...