2

With a lot of digging I have managed to share session between my 2 forms with the SHDocVw.dll. I have a Main form and a Popup form, each contain a WebBrowser control.

My question is : how can I handle the window.close javascript event to close the popup form ?

I think I have the correct syntax but I can't make it happen.

Main Form (webbrowser Navigator) :

    SHDocVw.WebBrowser nativeBrowser;
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        nativeBrowser = (SHDocVw.WebBrowser)Navigator.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 Form3();
        popup.Show(this);
        ppDisp = popup.webBrowser1.ActiveXInstance;
    }

Popup form

public Form3()
    {
        InitializeComponent();         
    }

    public WebBrowser Browser
    {
        get { return webBrowser1; }
    }

I think I have to add something like this to the popup code :

SHDocVw.WebBrowser nativeBrowser;

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    nativeBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    nativeBrowser.WindowClosing += nativeBrowser_closing;
}

protected void nativeBrowser_closing(bool IsChildWindow, ref bool Cancel)
{
    this.Close();
}

But when I declare the SHDocVw.WebBrowser nativeBrowser it opens a new IE instance instead of binding to the current one.

Can someone help me please :) ?

4

0 回答 0