.NET Framework 接收BeforeNavigate2事件BUG的替代方法

[.NET Framework 接收BeforeNavigate2事件BUG的替代方法]

????這是一篇微軟網絡上找到的文章,現在也許不適用了,但如果仍用.NET Framework 1.0,則只有這個方法可以解決這個問題了。


Liju Thomas [@online.microsoft.com]
Regarding BeforeNavigate2 it a bug as mentioned in the KB article.
But you can connect directly to IWebBrowserEvents (NOT IWebBrowserEvents2)
and sink to the BeforeNavigate event.

sample code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Data;
using SHDocVw;

namespace InetTest
{
????///
????/// Summary description for Form1.
????///
????public class Form1 : System.Windows.Forms.Form, DWebBrowserEvents
????{
????????private AxSHDocVw.AxWebBrowser axWebBrowser1;
????????private System.Windows.Forms.Button button1;
????????private System.Windows.Forms.Label label1;
????????private UCOMIConnectionPoint icp;
????????private int cookie = -1;

????????///
????????/// Required designer variable.
????????///
????????private System.ComponentModel.Container components = null;
????????private Object obj;
????????public Form1()
????????{
????????????//
????????????// Required for Windows Form Designer support
????????????//
????????????InitializeComponent();

????????????// Sink to webbrowser events
????????????UCOMIConnectionPointContainer icpc =
????????????????(UCOMIConnectionPointContainer)axWebBrowser1.GetOcx();

????????????Guid g = typeof(DWebBrowserEvents).GUID;
????????????icpc.FindConnectionPoint(ref g, out icp);
????????????icp.Advise(this, out cookie);
????????}
????????///
????????/// Clean up any resources being used.
????????///
????????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
????????///
????????/// Required method for Designer support - do not modify
????????/// the contents of this method with the code editor.
????????///
????????private void InitializeComponent()
????????{
????????????System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
????????????this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
????????????this.button1 = new System.Windows.Forms.Button();
????????????this.label1 = new System.Windows.Forms.Label();
????????????
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit()
;
????????????this.SuspendLayout();
????????????//
????????????// axWebBrowser1
????????????//
????????????this.axWebBrowser1.Enabled = true;
????????????this.axWebBrowser1.Location = new System.Drawing.Point(16, 8);
????????????this.axWebBrowser1.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxS
tate"));
????????????this.axWebBrowser1.Size = new System.Drawing.Size(456, 208);
????????????this.axWebBrowser1.TabIndex = 0;
????????????//
????????????// button1
????????????//
????????????this.button1.Location = new System.Drawing.Point(16, 232);
????????????this.button1.Name = "button1";
????????????this.button1.TabIndex = 1;
????????????this.button1.Text = "button1";
????????????this.button1.Click += new System.EventHandler(this.button1_Click);
????????????//
????????????// label1
????????????//
????????????this.label1.Location = new System.Drawing.Point(112, 232);
????????????this.label1.Name = "label1";
????????????this.label1.Size = new System.Drawing.Size(344, 23);
????????????this.label1.TabIndex = 2;
????????????this.label1.Text = "label1";
????????????//
????????????// Form1
????????????//
????????????this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
????????????this.ClientSize = new System.Drawing.Size(480, 273);
????????????this.Controls.AddRange(new System.Windows.Forms.Control[] {
???????????????????????????????????????????????????????????????????????? this.label1,
???????????????????????????????????????????????????????????????????????? this.button1,
???????????????????????????????????????????????????????????????????????? this.axWebBrowser1});
????????????this.Name = "Form1";
????????????this.Text = "Form1";
????????????
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
????????????this.ResumeLayout(false);

????????}
#endregion

????????// DWebBrowserEvents Implementation
????????public void BeforeNavigate(string url, int flags,
????????????string targetFrameName, ref object postData,
????????????string headers, ref bool cancel)
????????{
????????????label1.Text = "BeforeNavigate to " + url;
????????}
????????public void NavigateComplete(string url) { }
????????public void StatusTextChange(string text) { }
????????public void ProgressChange(int progress, int progressMax) { }
????????public void DownloadComplete() { }
????????public void CommandStateChange(int command, bool enable) { }
????????public void DownloadBegin() { }
????????public void NewWindow(string url, int flags, string targetFrameName, ref
object postData, string headers, ref bool processed) { }
????????public void TitleChange(string text) { }
????????public void FrameBeforeNavigate(string url, int flags, string
targetFrameName, ref object postData, string headers, ref bool cancel) { }
????????public void FrameNavigateComplete(string url) { }
????????public void FrameNewWindow(string url, int flags, string targetFrameName,
ref object postData, string headers, ref bool processed) { }
????????public void Quit(ref bool Cancel) { }
????????public void WindowMove() { }
????????public void WindowResize() { }
????????public void WindowActivate() { }
????????public void PropertyChange(string property) { }

????????///
????????/// The main entry point for the application.
????????///
????????[STAThread]
????????static void Main()
????????{
????????????Application.Run(new Form1());
????????}

????????private void button1_Click(object sender, System.EventArgs e)
????????{
????????????axWebBrowser1.Navigate ("www.msn.com", ref obj, ref obj, ref obj, ref
obj);
????????}
????}
}

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

(C)2001 Microsoft Corporation. All rights reserved.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章