How to display a popup from WebBrowser in another window that I created?

I am trying to implement a simple web browser control in one of my applications. This will help integrate the web application into the toolbox that I create.

The problem is that this web application absolutely loves pop-ups ....

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

How can I get any pop-ups created by clicking links in my WebBrowser to be a child of my MDI container (similar to setting the MDIParent property of a form)?

Thanks in advance.

+5
source share
3 answers

The web browser control supports the NewWindow event to receive pop-up notification. However, the Winforms shell does not allow you to do much, you can only cancel the pop-up window. Own COM-shell allows you to transfer a new instance of a web browser, this instance will then be used to display a pop-up window.

Using this requires some work. First, use the link "Project + Add Link", "Browse" and select c: \ windows \ system32 \ shdocvw.dll. This adds a link to the native COM interface.

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

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

Browser , - .

. WebBrowser :

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% , . , .

NewWindow2 , , . . Form2 Show(). Show(), , . , , MDI .

, , , Javascript alert(). HTML , .

+23

, - / NewWindow3

c:\windows\system32\shdocvw.dll, .

SHDocVw.WebBrowser wbCOMmain = (SHDocVw.WebBrowser)webbrowser.ActiveXInstance;
wbCOMmain.NewWindow3 += wbCOMmain_NewWindow3;

void wbCOMmain_NewWindow3(ref object ppDisp, 
                          ref bool Cancel, 
                          uint dwFlags, 
                          string bstrUrlContext, 
                          string bstrUrl)
{
    // bstrUrl is the url being navigated to
    Cancel = true; // stop the navigation

    // Do whatever else you want to do with that URL
    // open in the same browser or new browser, etc.
}
  • " " "Interop.SHDocVw" false
  • " " true.

MSDN

+2

By clarifying Hans' answer, you can get a WebBrowser to access COM without adding a link. This is using the unpublished methods of Winforms WebBrowser.AttachInterface and DetachInterface.

More details here .

Here is the code:

Usage (change instance of WebBrowser to WebBrowserNewWindow2)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.webBrowser1.NewWindow2 += webBrowser_NewWindow2;
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        webBrowser1.NewWindow2 -= webBrowser_NewWindow2;
        base.OnFormClosing(e);
    }

    void webBrowser_NewWindow2(object sender, WebBrowserNewWindow2EventArgs e)
    {
        var popup = new Form1();
        popup.Show(this);
        e.PpDisp = popup.Browser.ActiveXInstance;
    }
    public WebBrowserNewWindow2 Browser
    {
        get { return webBrowser1; }
    }
}

the code:

using System;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SHDocVw
{
    public delegate void WebBrowserNewWindow2EventHandler(object sender, WebBrowserNewWindow2EventArgs e);

    public class WebBrowserNewWindow2EventArgs : EventArgs
    {
        public WebBrowserNewWindow2EventArgs(object ppDisp, bool cancel)
        {
            PpDisp = ppDisp;
            Cancel = cancel;
        }

        public object PpDisp { get; set; }
        public bool Cancel { get; set; }
    }

    public class WebBrowserNewWindow2 : WebBrowser
    {
        private AxHost.ConnectionPointCookie _cookie;
        private WebBrowser2EventHelper _helper;

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();

            _helper = new WebBrowser2EventHelper(this);
            _cookie = new AxHost.ConnectionPointCookie(
                this.ActiveXInstance, _helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void DetachSink()
        {
            if (_cookie != null)
            {
                _cookie.Disconnect();
                _cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNewWindow2EventHandler NewWindow2;

        private class WebBrowser2EventHelper : StandardOleMarshalObject, DWebBrowserEvents2
        {
            private readonly WebBrowserNewWindow2 _parent;

            public WebBrowser2EventHelper(WebBrowserNewWindow2 parent)
            {
                _parent = parent;
            }

            public void NewWindow2(ref object pDisp, ref bool cancel)
            {
                WebBrowserNewWindow2EventArgs arg = new WebBrowserNewWindow2EventArgs(pDisp, cancel);
                _parent.NewWindow2(this, arg);
                if (pDisp != arg.PpDisp)
                    pDisp = arg.PpDisp;
                if (cancel != arg.Cancel)
                    cancel = arg.Cancel;
            }
        }

        [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
        TypeLibType(TypeLibTypeFlags.FHidden)]
        public interface DWebBrowserEvents2
        {
            [DispId(0xfb)]
            void NewWindow2(
                [In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp,
                [In, Out] ref bool cancel);
        }
    }
}
+1
source

All Articles