最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Window.Opener Object Null on .NET WebBrowser Control New Window - Stack Overflow

programmeradmin2浏览0评论

Well I have a Pop up in the WebBroswer control of the .NET Framework that I capture with the NewWindow event handler like so.

WebBrowser w = new WebBrowser();
SHDocVw.WebBrowser_V1 web = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
web.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(web_NewWindow);

The new Popup is in a new form with a new instance of the WebBrowser control.

void web_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        Processed = true;
        WebBrowser w2 = new WebBrowser();

        Form PopUp = new Form();
        PopUp.Controls.Clear();
        PopUp.Controls.Add(w2);
        w2.Dock = DockStyle.Fill;
        PopUp.Show();
        w2.Navigate(URL);
        w2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w2_DocumentCompleted);
    }

My problem is that the Window.Opener object is null in the process.

The page uses that field to return the user back to the original window and fills some values on the form.

Is there anyway to pass the Window.Opener object from one WebBrowser control to the next? Seems impossible that this will work but I have to try.

Thanks

Well I have a Pop up in the WebBroswer control of the .NET Framework that I capture with the NewWindow event handler like so.

WebBrowser w = new WebBrowser();
SHDocVw.WebBrowser_V1 web = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
web.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(web_NewWindow);

The new Popup is in a new form with a new instance of the WebBrowser control.

void web_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        Processed = true;
        WebBrowser w2 = new WebBrowser();

        Form PopUp = new Form();
        PopUp.Controls.Clear();
        PopUp.Controls.Add(w2);
        w2.Dock = DockStyle.Fill;
        PopUp.Show();
        w2.Navigate(URL);
        w2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w2_DocumentCompleted);
    }

My problem is that the Window.Opener object is null in the process.

The page uses that field to return the user back to the original window and fills some values on the form.

Is there anyway to pass the Window.Opener object from one WebBrowser control to the next? Seems impossible that this will work but I have to try.

Thanks

Share Improve this question edited Jun 27, 2009 at 1:20 Proximo asked Jun 26, 2009 at 21:29 ProximoProximo 6,55111 gold badges53 silver badges68 bronze badges 2
  • "like so"? Looks like you for got to post the code. Please post it. – John Saunders Commented Jun 26, 2009 at 23:57
  • Alright, just a sec I didn't notice that – Proximo Commented Jun 27, 2009 at 0:06
Add a ment  | 

2 Answers 2

Reset to default 5

Yes, there is way to pass the Window.Opener object from one WebBrowser control to the next.
See code below for details.

public class WebBrowserTest
{
    private WebBrowser _wParent;

    // entry point
    public void Navigate(string url)
    {
        _wParent = new WebBrowser();
        SHDocVw.WebBrowser_V1 v1 = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
        v1.NewWindow += v1_NewWindow;
    }

    private void v1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        // tell, that we'll open new page by ourselves
        Processed = true;

        // open page by ourselves
        WebBrowser w2 = new WebBrowser();
        w2.Navigate(url);

        // wait for document will be loaded to do some magic stuff then
        w2.DocumentCompleted += w2_DocumentCompleted;
    }

    // sets opener for popup window
    // after document in popup window was loaded
    private void w2_DocumentCompleted(object sender, EventArgs e)
    {
        WebBrowser popup = (WebBrowser)sender;
        SetOpener(_wparent, popup);
    }

    // My most favorite method :)
    // Contains exactly that hack, we're talking about
    private void SetOpener(WebBrowser opener, WebBrowser popup)
    {    
        HtmlWindow htmlPopup = popup.Document.Window;
        HtmlWindow htmlOpener = opener.Document.Window;

        // let the dark magic begin

        // actually, WebBrowser control is .NET wrapper around IE COM interfaces
        // we can get a bit closer to them access by getting reference to 
        // "mshtml.IHTMLWindow2" field via Reflection
        FieldInfo fi = htmlPopup.GetType().GetField("htmlWindow2", BindingFlags.Instance | BindingFlags.NonPublic);

        mshtml.IHTMLWindow2 htmlPopup2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlPopup);
        mshtml.IHTMLWindow2 htmlOpener2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlOpener);

        // opener is set here
        htmlPopup2.window.opener = htmlOpener2.window.self;
    }
}

p.s. I understand, chances, that it's still actual, are very small, but, anyway =)

For those who are looking for WPF version of the solution, rewrite the SetOpener function to below:

public static void SetOpener(System.Windows.Controls.WebBrowser opener, System.Windows.Controls.WebBrowser popup)
{
    mshtml.IHTMLWindow2 htmlPopup = (popup.Document as mshtml.HTMLDocument).parentWindow;
    mshtml.IHTMLWindow2 htmlOpener = (opener.Document as mshtml.HTMLDocument).parentWindow;
    htmlPopup.window.opener = htmlOpener.window.self;
}

Use it in event handler on the child side WebBrowser:

private void browser_LoadCompleted(object sender, NavigationEventArgs eventArgs)
        {
            WebBrowser popup = (WebBrowser)sender;
            SetOpener(parentWebBrowser, popup);
        }

And the WPF part:

<Grid>
    <WebBrowser Name="webBrowser" LoadCompleted="browser_LoadCompleted" />
</Grid>

Hope I just saved someone else from hours of trial, error and endless googling.

发布评论

评论列表(0)

  1. 暂无评论