如何显示在我创造了另一个窗口中的web浏览器的弹出?

如何显示在我创造了另一个窗口中的web浏览器的弹出?

问题描述:

我想实现我的应用程序的一个简单的Web浏览器控件。这是为了帮助一个Web应用程序集成到一个工具,我创建。

I am trying to implement a simple web browser control in one of my apps. This is to help integrate a web app into a toolset i am creating.

问题是,这个Web应用程序是绝对喜欢弹出窗口....

The problem is, this web app absolutly loves popup windows....

在弹出打开时,它是不MDI容器的形式,我的主窗口的一部分的孩子一个IE窗口中打开。

When a popup is opened, it opens in an IE window which is not a child of the MDI Container form that my main window is part of.

我怎样才能通过点击链接在我的web浏览器成为我的MDI容器的子(类似于设置窗体的MdiParent属性)创建的任何和所有弹出窗口?

How can i get any and all popups created by clicking links in my WebBrowser to be a child of my MDI container (similar to setting the MDIParent property of a form)?

先谢谢了。

Web浏览器控件支持NewWindow事件收到通知的弹出窗口。该包装的WinForms但是不会让你做多少,你只能取消弹出。本机COM包装允许回传网络浏览器的新实例,该实例将被用来显示弹出式窗口。

The web browser control supports the NewWindow event to get notified about a popup window. The Winforms wrapper however does not let you do much with it, you can only cancel the popup. The native COM wrapper permits passing back a new instance of the web browser, that instance will then be used to display the popup.

这个趁着需要做一些工作。对于初学者来说,使用Project +添加引用,浏览选项卡,选择C:\\ WINDOWS \\ SYSTEM32 \\ shdocvw.dll中。这增加了原生的COM接口的引用。

Taking advantage of this requires some work. For starters, use Project + Add Reference, Browse tab and select c:\windows\system32\shdocvw.dll. That adds a reference to the native COM interface.

创建作为弹出窗体的窗体。砸就可以了web浏览器,并使其code类似于此:

Create a form that acts as the popup form. Drop a WebBrowser on it and make its code look similar to this:

public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();
    }
    public WebBrowser Browser {
        get { return webBrowser1; }
    }
}

浏览器属性可以访问将被用于在弹出窗口中显示的网页浏览器。

The Browser property gives access to the browser that will be used to display the web page in the popup window.

现在回到主窗体。砸就可以了web浏览器,并使其code是这样的:

Now back to the main form. Drop a WebBrowser on it and make its code look like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        webBrowser1.Url = new Uri("http://google.com");
    }
    SHDocVw.WebBrowser nativeBrowser;
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        nativeBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
        nativeBrowser.NewWindow2 += nativeBrowser_NewWindow2;
    }
    protected override void OnFormClosing(FormClosingEventArgs e) {
        nativeBrowser.NewWindow2 -= nativeBrowser_NewWindow2;
        base.OnFormClosing(e);
    }

    void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel) {
        var popup = new Form2();
        popup.Show(this);
        ppDisp = popup.Browser.ActiveXInstance;
    }
}

在onload方法获取到本机COM接口的引用,然后订阅事件处理程序NewWindow2事件。我确信退订该事件中的FormClosing事件处理程序,不是100%肯定,如果这是必要的。更好的安全然后抱歉。

The OnLoad method obtains a reference to the native COM interface, then subscribes an event handler to the NewWindow2 event. I made sure to unsubscribe that event in the FormClosing event handler, not 100% sure if that's necessary. Better safe then sorry.

NewWindow2事件处理程序是症结所在,注意第一个参数允许回传非类型化的参考。这应该是在弹出的窗口中本地浏览器。所以,我创建窗体2和show()它的一个实例。注意参数显示(),即确保弹出是一家拥有窗口。替代这次需要为您的应用程序,我想你会希望你的情况来创建一个MDI子窗口。

The NewWindow2 event handler is the crux, note that the first argument allows passing back an untyped reference. That should be the native browser in the popup window. So I create an instance of Form2 and Show() it. Note the argument to Show(), that ensures that the popup is an owned window. Substitute this as necessary for your app, I assume you'd want to create an MDI child window in your case.

,一定要提防这个事件不火的窗口中显示时,JavaScript使用警报()。浏览器不把那个窗口作为HTML弹出,并且不使用浏览器窗口来显示它,所以你不能拦截或更换。

Do beware that this event doesn't fire for the window displayed when Javascript uses alert(). The browser doesn't treat that window as an HTML popup and doesn't use a browser window to display it so you cannot intercept or replace it.