Hooking the IE BeforeNavigate2 Event (2009-10-01)
sing C# (and it must be C#), I'm trying to...
1) Create an instance of IE.
2) Intercept the NewWindow2 event of the IE instance created in step #1
3) In the NewWindow2 event processing, create a new IE window.
4) Intercept the BeforeNavigate2 event for the IE window created in step #2.
Everything's working except step #4. The BeforeNavigate2 event handler
doesn't get called when a popup is invoked. (Though I *can* successfully
hook into the BeforeNavigate2 event of the first IE window.)
Ten PayPal dollars and my everlasting gratitude to the first person who
either fixes my error, points me to a patch, or proves that it is a
Frameworks bug. (seriously)
Here's the code:
--------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using SHDocVw;
/*
* To run this program, you must add the Microsoft
* Internet Controls (SHDocVw.dll) to the list of
* references.
*
* This program adapted from MSDN article:
*
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconhandlingeventsraisedbycomsource.asp
*/
namespace InternetExplorerPlay
{
public class IE
{
public static void Main()
{
IE explorer = new IE();
explorer.Run();
}
public void Run()
{
Object o = null;
String s;
try
{
// Start the browser.
ie = new SHDocVw.InternetExplorer();
}
catch(Exception e)
{
Console.WriteLine("Exception when creating IE object {0}", e);
return;
}
// Wire your event handlers to the ie instance.
SetAllEvents();
try
{
// Go to a page.
wb = (IWebBrowserApp) ie;
wb.Visible = true;
// click the "Live Preview!" link to popup a window
wb.Navigate("http://javascriptkit.com/popwin/index.shtml", ref o,
ref o, ref o, ref o);
// Start navigating to different URLs.
Console.WriteLine("Enter URL (or enter to quit):");
s = Console.ReadLine();
while(s != "" && ie != null && wb != null)
{
wb.Navigate(s, ref o, ref o, ref o, ref o);
Console.WriteLine("Enter URL (or enter to quit):");
s = Console.ReadLine();
}
wb.Quit();
}
catch(Exception sE)
{
Console.WriteLine("Exception happens {0}", sE);
}
}
//Use the += syntax for adding delegates to events.
void SetAllEvents()
{
if (ie != null)
{
DWebBrowserEvents2_NewWindow2EventHandler DNewWindowChangeE =
new DWebBrowserEvents2_NewWindow2EventHandler(OnNewWindow2);
ie.NewWindow2 += DNewWindowChangeE;
}
}
static void OnNewWindow2(ref Object o, ref bool cancel)
{
Console.WriteLine("OnNewWindow2 called.");
// create a new window and hook into the BeforeNavigate2 event
ie2 = new SHDocVw.InternetExplorer();
DWebBrowserEvents2_BeforeNavigate2EventHandler
DBeforeNavigateE = new
DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);
ie2.BeforeNavigate2 += DBeforeNavigateE;
ie2.Visible = true;
o = ie2;
cancel = false;
return;
}
// **** this handler is never invoked ****
static void OnBeforeNavigate2(Object ob1, ref Object URL,
ref Object Flags, ref Object Name, ref Object da,
ref Object Head, ref bool Cancel)
{
Console.WriteLine("Before Navigate2");
Cancel = false;
}
static private SHDocVw.InternetExplorer ie = null;
static private IWebBrowserApp wb = null;
static private SHDocVw.InternetExplorer ie2 = null;
}
}
--------------------------------------------------------
---
Mark Harris
mtharris@yahoo.com
 05-03-2002, 10:39 AM
|
|
|
|
Re: Hooking the IE BeforeNavigate2 Event
Mark,
Could it be related to this one?
BUG: BeforeNavigate2 Event of WebBrowser Control Does Not Fire If
Hosted in Visual Studio .NET Application
http://support.microsoft.com/default...;EN-US;q311298
Mattias
===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/
|
|