Get URL for WebBrowser.NewWindow Event [duplicate]

牧云@^-^@ 提交于 2020-02-20 05:40:31

问题


I'm trying to redirect new Window event to a new tab:

myWebBrowser.NewWindow += add_NewTab; 

//...

private void add_NewTab(object sender, CancelEventArgs e)
{ 
    WebBrowser thisWebBrowser = (WebBrowser)sender;
    e.Cancel = true; //should block the default browser to open a new window

    TabPage addedTabPage = new TabPage("redirected tab"); //create a new tab
    tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the new tab to the TabControl
    WebBrowser addedWebBrowser = new WebBrowser() //create the new web browser inside the new tab
    {
        Parent = addedTabPage,
        Dock = DockStyle.Fill
    };

    addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString()); //set the new browser destination url
}

I'm not sure that using WebBrowser.StatusText is the best way to obtain the new window url (this doesn't work for every site I've tested).

Is there a better class/method to call to get new window destination instead?


UPDATE:

I've tried the solution suggested by Charlie

  1. added the Microsoft Internet Control (COM) reference
  2. added using SHDocVw;
  3. used the code:

    System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser();
    SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)myWebBrowser.ActiveXInstance;
    axBrowser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);

Unfortunately I've received a NullReference Exception on third line that I wasn't able to correct.


SOLUTION:

I don't think the solution is in the related answer (or I wasn't able to find it) because it explains how to implement the NewWindow2 event (instead of NewWindow3 which handles the original destination url) and the implementation is the same suggested here which leads to the NullReference Exception error.
Anyway I've discovered this two posts:

  • the discussion
  • the original article (the page is in Chinese but the code is readable)

The suggest is to modify the previous three lines into this one:

(myWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);

Everything is working now and I was able to keep using the original System.Windows.Forms.WebBrowser in all the rest of the code.


回答1:


It looks like the WebBrowser control is a really lame wrapper around SHDocVw. Fortunately Microsoft exposes the underlying implementation through WebBrowser.ActiveXInstance.

This code from http://www.codeproject.com/Articles/71592/How-to-easily-capture-the-NewWindow3-event-and-det will do the trick:

First, add a reference to Microsoft Internet Controls. Then implement a NewWindow3 handler:

SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
axBrowser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);


来源:https://stackoverflow.com/questions/14871235/get-url-for-webbrowser-newwindow-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!