Sometimes we want to intercept an IE window event in an application. In most cases, we will think of BHO, but it is inconvenient to install BHO, and it is easy to be cleaned up by anti-virus software as a Trojan. In fact, there is a better way to use the shellwindows object in VB. Next, I wrote a simple ie window event monitoring Class Based on the shellwindows object. The Code is as follows:
'*************************************** * <Br/> '* Class Name: iehooker <br/> '* class function: monitors Open and Close events in IE Windows <br/>' * Author: lyserver <br/> '* remarks: you need to reference the Microsoft Internet controls class library <br/> '***************************** * ********** </P> <p> Option explicit </P> <p> dim withevents m_shwindows as shellwindows <br/> dim m_objie () as internetexplorer <br/> dim m_niecount as long </P> <p> '------------------------------------ <br/>' process description: class initialization <br/> '------------------------------------ <br/> private sub class_initialize () <br/> dim objie as internetexplorer </P> <p> 'Add the opened IE Object to the array. <br/> set m_shwindows = new shellwindows <br/> for each objie in m_shwindows <br/> If instr (objie. fullname, "/iexplore.exe") Then <br/> redim preserve m_objie (m_niecount) <br/> set m_objie (m_niecount) = objie <br/> m_niecount = m_niecount + 1 <br/> end if <br/> next <br/> end sub </P> <p> '-------------------------------- <br /> 'process description: class destruction <br/> '------------------------------------ <br/> private sub class_terminate () <br/> dim I as long </P> <p> 'Destroy an array <br/> for I = 0 to m_niecount-1 <br/> set m_objie (I) = nothing <br/> next <br/> erase m_objie <br/> end sub </P> <p> '---------------------------------- <br/>' process description: open the IE window event <br/> '---------------------------------- <br/> private sub m_shwindows_windowregistered (byval lcookie as long) <br/> 'Add the IE Object to be opened to the array. <br/> If instr (m_shwindows.item.fullname, "/iexplore.exe ") = 0 Then exit sub <br/> redim preserve m_objie (m_niecount) <br/> set m_objie (m_niecount) = m_shwindows.item <br/> m_niecount = m_niecount + 1 <br/> end sub </P> <p> '---------------------------------- <br/>' process description: IE window close event <br/> '------------------------------------ <br/> private sub m_shwindows_windowrevoked (byval lcookie as long) <br/> dim I as long <br/> dim blnfound as Boolean </P> <p> 'removes the IE OBJECT To be closed from the array. <br/> If instr (m_shwindows.item.fullname, "/iexplore.exe") = 0 Then exit sub <br/> for I = 0 to m_niecount-1 <br/> If m_objie (I ). hwnd = m_shwindows.item.hwnd then blnfound = true <br/> If blnfound then <br/> If I = m_niecount-1 then exit for <br/> set m_objie (I) = m_objie (I + 1) <br/> end if <br/> next <br/> m_niecount = m_niecount-1 <br/> If m_niecount> 0 then redim preserve m_objie (m_niecount-1) <br/> end sub <br/>
The test code is as follows:
Dim objiehooker as iehooker </P> <p> private sub form_load () <br/> set objiehooker = new iehooker <br/> end sub </P> <p> private sub form_unload (cancel as integer) <br/> set objiehooker = nothing <br/> end sub <br/>
This class is just an example and does not monitor more events. You can add the events you want to monitor based on Dom, such as intercepting the right-click menu. To intercept DOM events, you need to add an event class. The Code is as follows:
'*************************************** * <Br/> '* Class Name: ieevent <br/> '* function: IE event <br/>' * Author: lyserver <br/> '* remarks: you need to reference Microsoft Internet controls <br/> '* and Microsoft HTML Object Library <br/> '****************** * ******************** </P> <p> Option explicit <br/> private declare function MessageBox lib "USER32" alias "messageboxa" (byval hwnd as long, byval lptext as string, byval lpcaption as string, byval wtype as long) as long </P> <p> Public withevents m_objie as internetexplorer <br/> Public withevents m_objdoc as htmldocument </P> <p> '-example <br/>' process description: class initialization <br/> '------------------------------------ <br/> private sub class_initialize () <br/> '<br/> end sub </P> <p>'------------------------------------ <br/> 'process description: class destruction <br/> '------------------------------------ <br/> private sub class_terminate () <br/> set m_objdoc = nothing <br/> set m_objie = nothing <br/> end sub </P> <p> '---------------------------------- <br/>' function description: IE context menu event <br/> '-warning <br/> private function m_objdoc_oncontextmenu () as Boolean <br/> MessageBox m_objie.hwnd, "ie context menu intercepted ", "test ", 0 <br/> m_objdoc_oncontextmenu = true' continue to display right-click menu <br/> end function </P> <p> '-------------------------------- <br/>' process description: IE File loading completion event <br/> '------------------------------------ <br/> private sub m_objie_documentcomplete (byval Pdisp as object, URL as variant) <br/> set m_objdoc = m_objie.document <br/> end sub </P> <p> '---------------------------------- <br/>' process description: IE window close event <br/> '---------------------------------- <br/> private sub m_objie_onquit () <br/> set m_objie = nothing <br/> end sub <br/>
The event monitoring code is modified as follows:
'*************************************** * <Br/> '* Class Name: iehooker <br/> '* class function: IE event monitoring class <br/>' * Author: lyserver <br/> '* remarks: you need to reference the Microsoft Internet controls class library <br/> '***************************** * ********** </P> <p> Option explicit </P> <p> dim withevents m_shwindows as shellwindows <br/> dim m_ieevent () as ieevent <br/> dim m_niecount as long </P> <p> '------------------------------------ <br/>' process description: class initialization <br/> '------------------------------------ <br/> private sub class_initialize () <br/> dim objie as internetexplorer </P> <p> 'Add the opened IE Object to the array. <br/> set m_shwindows = new shellwindows <br/> for each objie in m_shwindows <br/> If instr (objie. fullname, "/iexplore.exe") Then <br/> redim preserve m_ieevent (m_niecount) <br/> set m_ieevent (m_niecount) = new ieevent <br/> set m_ieevent (m_niecount ). m_objie = objie <br/> set m_ieevent (m_niecount ). m_objdoc = objie. document <br/> m_niecount = m_niecount + 1 <br/> end if <br/> next <br/> end sub </P> <p> '---------------------------------- <br/> 'process description: class destruction <br/> '------------------------------------ <br/> private sub class_terminate () <br/> dim I as long </P> <p> 'Destroy an array <br/> for I = 0 to m_niecount-1 <br/> set m_ieevent (I) = nothing <br/> next <br/> erase m_ieevent <br/> end sub </P> <p> '---------------------------------- <br/>' process description: open the IE window event <br/> '---------------------------------- <br/> private sub m_shwindows_windowregistered (byval lcookie as long) <br/> dim objie as internetexplorer </P> <p> 'Add the IE Object to be opened to the array. <br/> set objie = m_shwindows (m_shwindows.count-1) <br/> If instr (objie. fullname, "/iexplore.exe") = 0 Then exit sub <br/> redim preserve m_ieevent (m_niecount) <br/> set m_ieevent (m_niecount) = new ieevent <br/> set m_ieevent (m_niecount ). m_objie = objie <br/> m_niecount = m_niecount + 1 <br/> end sub </P> <p> '-------------------------------- <br/>' process description: IE window close event <br/> '------------------------------------ <br/> private sub m_shwindows_windowrevoked (byval lcookie as long) <br/> dim I as long <br/> dim blnfound as Boolean </P> <p> 'removes the ie object To be closed from the array. <br/> for I = 0 to m_niecount-1 <br/> If m_ieevent (I ). m_objie is nothing then blnfound = true <br/> If blnfound then <br/> If I = m_niecount-1 then exit for <br/> set m_ieevent (I) = m_ieevent (I + 1) <br/> end if <br/> next <br/> m_niecount = m_niecount-1 <br/> If m_niecount> 0 then redim preserve m_ieevent (m_niecount-1) <br/> end sub <br/>
The test code remains unchanged.
After the project is run, right-click the HTML document area in any ie window and a dialog box is displayed.