Drew Chapin
Drew Chapin

Reputation: 7989

NewWindow3 Event Not Fired

I'm trying to use the NewWindow3 event with the Microsoft Web Browser control so that I can capture the URL and prevent it from opening Internet Explorer. NewWindow2 fires fine, but NewWindow3 does not. The only relevent thing I can find is NewWindow3 event on extended WebBrowser, but I don't want to use a custom control. I want to use the stock control. What can I do to get the NewWindow3 event to work without using a custom control?

private void Form1_Load( object sender, EventArgs e )
{
    webBrowser1.Navigate("http://www.stackoverflow.com");
    SHDocVw.WebBrowser browser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    Debug.Assert(browser != null);
    browser.NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(browser_NewWindow2);
    browser.NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(browser_NewWindow3);
}

void browser_NewWindow2( ref object ppDisp, ref bool Cancel )
{
    Debug.Write("NewWindow2");
}

private void browser_NewWindow3( ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl )
{
    Debug.Write(bstrUrl);
}

Upvotes: 2

Views: 2680

Answers (3)

Lucius
Lucius

Reputation: 2872

As explained in this post, try to set the properties Embed Interop Type of the reference SHDocVw to false.

Upvotes: 0

Limitless isa
Limitless isa

Reputation: 3802

enter image description here

    // Form_Load
this.axWebBrowser1.NewWindow3 += new AxSHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(this.axWebBrowser1_NewWindow3);
this.axWebBrowser1.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.axWebBrowser1_NewWindow2);

object obj = null; 


private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
      e.cancel = true;
}

private void axWebBrowser1_NewWindow3(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e)
{
      //MessageBox.Show(e.bstrUrl);
      Form1 nF = new Form1();
      nF.axWebBrowser1.RegisterAsBrowser = true;
      e.ppDisp = nF.axWebBrowser1.Application;
      nF.Visible = true;
      nF.axWebBrowser1.Navigate(e.bstrUrl, ref obj, ref obj, ref obj, ref obj);
      e.cancel = true;
}

Upvotes: 1

Drew Chapin
Drew Chapin

Reputation: 7989

After weeks of trying to figure this out, I finally figured it out. Rather than using the .Net wrapper of the Web Browser control, I added the Web Browser Control as an ActiveX COM control by doing the following:

  • Right click on any control in the Toolbox, and select Choose Items...
  • Go to COM Components tab and select Microsoft Web Browser
  • From the All Windows Forms category of the Toolbox, add a Microsoft Web Browser control to the form.
  • The NewWindow3 event is exposed normally. No need to use webBrowser1.AxtiveXControl to get to it.
  • Add code for NewWindow3 event

    private void axWebBrowser1_NewWindow3( object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e )
    {
        Debug.WriteLine(e.bstrUrl);
        e.cancel = true;
    }
    

Upvotes: 2

Related Questions