RESOLUTION
To work around this problem, hook up event handlers to the
non-default source interface, in this case, the DWebBrowserEvents interface.
This means you have to implement the DWebBrowserEvents and its methods (of
which there are 17).
public void BeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel)
{
MessageBox.Show("C# DWebBrowser::BeforeNavigate event fired!", "DWebBrowser Event");
}
public void PropertyChange(string Property){}
public void NavigateComplete(string URL){}
public void WindowActivate(){}
public void FrameBeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel){}
public void NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed){}
public void FrameNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed){}
public void TitleChange(string Text){}
public void DownloadBegin(){}
public void DownloadComplete(){}
public void WindowMove(){}
public void WindowResize(){}
public void Quit(ref bool Cancel){}
public void ProgressChange(int Progress, int ProgressMax){}
public void StatusTextChange(string Text){}
public void CommandStateChange(int Command, bool Enable){}
public void FrameNavigateComplete(string URL){}
COM classes advertise that they support events by implementing an
interface named IConnectionPointContainer, and by returning connection point
objects by means of this interface. Connection point objects, which implement
IConnectionPoint, are provided by the connectable objects, representing a
collection of events.
The System.Runtime.InteropServices namespace
defines common COM interfaces, such as IConnectionPointContainer and
IConnectionPoint. These definitions can be confusing because each interface has
been renamed with the prefix "UCOM" added. The "U" indicates unmanaged; "COM"
indicates that the interface is originally defined in COM.
Under
InteropServices, the IConnectionPointContainer and IConnectionPoint interfaces
are defined as follows:
- UCOMIConnectPointContainer
- UCOMIConnectionPoint
Although the names of the interfaces are different, COM
recognizes no difference between a "UCOM" interface and its corresponding
unmanaged definition.
When developing code, you must do the
following:
- Obtain the IConnectionPointContainer interface.
private UCOMIConnectionPoint icp;
UCOMIConnectionPointContainer icpc = (UCOMIConnectionPointContainer)axWebBrowser1.GetOcx();
- Obtain the IConnectionPoint interface for a specified IID
(event interface they want to subscribe to) by using the IConnectionPointContainer::FindConnectinPoint method.
Guid g = typeof(DWebBrowserEvents).GUID;
icpc.FindConnectionPoint(ref g, out icp);
Using the IConnectionPoint interface Advise and Unadvise methods, clients can hook and unhook callback interface pointers.
A client calls IConnectionPoint::Advise with the IUnknown pointer to itself, and then Advise passes back an integral cookie that uniquely identifies the
connection. On a successful return, Advise contains the connection cookie. The pdwCookie value must be unique for each connection to any particular
instance of a connection point. - Use the IConnectionPoint::Advise method to establish a connection between the connection point
object and the client's sink.
icp.Advise(this, out cookie);
- Use the IConnectionPoint::Unadvise method to disconnect a connection between the connection point
object and the client's sink.
icp.Unadvise(cookie);
Visual C# .NET Sample Code:
Paste the following code into a Visual C# .NET Windows form
module:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using SHDocVw;
using System.Runtime.InteropServices;
namespace wbcs
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form, DWebBrowserEvents
{
private UCOMIConnectionPoint icp;
private int cookie = -1;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
UCOMIConnectionPointContainer icpc = (UCOMIConnectionPointContainer)axWebBrowser1.GetOcx(); // ADDed
Guid g = typeof(DWebBrowserEvents).GUID;
icpc.FindConnectionPoint(ref g, out icp);
icp.Advise(this, out cookie);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
// Release event sink
if (-1 != cookie) icp.Unadvise(cookie);
cookie = -1;
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support; do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// axWebBrowser1
//
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(8, 16);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(448, 240);
this.axWebBrowser1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.axWebBrowser1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
object obj = null;
axWebBrowser1.Navigate ("http://www.microsoft.com", ref obj, ref obj, ref obj, ref obj);
}
public void BeforeNavigate(string URL, int Flags, string TargetFrameName,
ref object PostData, string Headers, ref bool Cancel)
{
MessageBox.Show("C# DWebBrowser::BeforeNavigate event fired!", "DWebBrowser Event");
}
public void PropertyChange(string Property){}
public void NavigateComplete(string URL){}
public void WindowActivate(){}
public void FrameBeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel){}
public void NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed){}
public void FrameNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed){}
public void TitleChange(string Text){}
public void DownloadBegin(){}
public void DownloadComplete(){}
public void WindowMove(){}
public void WindowResize(){}
public void Quit(ref bool Cancel){}
public void ProgressChange(int Progress, int ProgressMax){}
public void StatusTextChange(string Text){}
public void CommandStateChange(int Command, bool Enable){}
public void FrameNavigateComplete(string URL){}
}
}