好吧,我在.NET Framework的WebBroswer控件中弹出了一个窗口,该窗口是通过NewWindow事件处理程序捕获的,就像这样。
WebBrowser w = new WebBrowser();
SHDocVw.WebBrowser_V1 web = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
web.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(web_NewWindow);
新的Popup带有WebBrowser控件的新实例的新形式。
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);
}
我的问题是Window.Opener对象在该过程中为null。
页面使用该字段将用户返回到原始窗口,并在表单上填写一些值。
无论如何,是否要将Window.Opener对象从一个WebBrowser控件传递到下一个?看来这行不通,但我必须尝试。
谢谢
最佳答案
是的,有一种方法可以将Window.Opener对象从一个WebBrowser控件传递到下一个。
有关详细信息,请参见下面的代码。
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.我了解,仍然很实际的机会很小,但是无论如何=)
关于.net - NET WebBrowser控件上的Javascript Window.Opener对象为null新窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1051358/