获取WebBrowser.NewWindow事件的URL [英] Get URL for WebBrowser.NewWindow Event

查看:337
本文介绍了获取WebBrowser.NewWindow事件的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将新的Window事件重​​定向到新的标签页:

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
}

我不确定使用WebBrowser.StatusText是获取新窗口网址的最佳方法(这不适用于我测试过的每个网站).

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?

更新:

我已经尝试过查理(Charlie)建议的解决方案

I've tried the solution suggested by Charlie

  1. 添加了Microsoft Internet Control(COM)参考
  2. 使用SHDocVw添加;
  3. 使用了代码:

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

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

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

很遗憾,我在第三行收到了我无法纠正的NullReference Exception.

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

解决方案:

我不认为解决方案在相关答案中(或者我找不到),因为它说明了如何实现NewWindow2事件(而不是用于处理原始目标URL的NewWindow3),并且该实现是此处建议的结果相同,这会导致NullReference Exception错误.
无论如何,我发现了这两篇文章:

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);

现在一切正常,我能够在其余所有代码中继续使用原始的System.Windows.Forms.WebBrowser.

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

推荐答案

看来WebBrowser控件确实是围绕SHDocVw的la脚包装.幸运的是,Microsoft通过WebBrowser.ActiveXInstance公开了基础实现.

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

此代码来自 http://www.codeproject.com/Articles/71592/How-to-easily-capture-the-NewWindow3-event-and-det 将达到目的:

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

首先,添加对Microsoft Internet Controls的引用.然后实现一个NewWindow3处理程序:

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);

这篇关于获取WebBrowser.NewWindow事件的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆