在用c# WebBrowser扩展控件时,对事件NewWindow3进行了处理:
本来要在新的IE窗口打开的页面进行了吸收( e.cancel = true;)
再用WebBrowser打开新页面。
因吸收了页面,[打开的新页面]与[原页面]的联系没有了。
本来在IE中[打开的新页面]可以通过window.opener访问[原页面]的window,
现在因为是WebBrowser打开新页面,页面window.opener值为NULL.
请问高手,该怎样处理window.opener值为[打开新页面的页面]的window.
问题整理如下:
1、目前在学习使用WebBrowser控件的扩展类ExtendedWebBrowser做浏览器
2、在弹出新页面时,设置了e.cancel=true,即只在一个webbrowser中打开
3、要浏览的目标页面上存在window.opener或window.parent等JS
4、使用两个webbrowser打开时,无法将这两个webbrowser关联起来,所以window.opener执行时会报错。若正确关联的话,应该是能够正常执行的。
问题是:ExtendedWebBrowser如何是两个webbrowser关联起来?
1 /// <summary> 2 /// 扩展Webbrowser类 3 /// </summary> 4 public class ExtendedWebBrowser : WebBrowser 5 { 6 /// <summary> 7 /// components 8 /// </summary> 9 private IContainer components; 10 11 /// <summary> 12 /// cookie 13 /// </summary> 14 private AxHost.ConnectionPointCookie cookie; 15 16 /// <summary> 17 /// events 18 /// </summary> 19 private WebBrowserExtendedEvents events; 20 21 /// <summary> 22 /// 构造 23 /// </summary> 24 public ExtendedWebBrowser() 25 { 26 } 27 28 /// <summary> 29 /// 事件属性-当前窗口 30 /// </summary> 31 public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate; 32 33 /// <summary> 34 /// 事件属性-新开窗口 35 /// </summary> 36 public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow; 37 38 public object Application; 39 40 /// <summary> 41 /// IDWebBrowserEvents2 42 /// </summary> 43 [ComImport, TypeLibType(TypeLibTypeFlags.FHidden), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")] 44 public interface IDWebBrowserEvents2 45 { 46 /// <summary> 47 /// BeforeNavigate2 48 /// </summary> 49 /// <param name="pdisp">pdisp</param> 50 /// <param name="url">url</param> 51 /// <param name="flags">flags</param> 52 /// <param name="targetFrameName">targetFrameName</param> 53 /// <param name="postData">postData</param> 54 /// <param name="headers">headers</param> 55 /// <param name="cancel">cancel</param> 56 [DispId(250)] 57 void BeforeNavigate2([In, MarshalAs(UnmanagedType.IDispatch)] object pdisp, [In] ref object url, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers, [In, Out] ref bool cancel); 58 59 ///// <summary> 60 ///// NewWindow2 61 ///// </summary> 62 ///// <param name="pdisp">pdisp</param> 63 ///// <param name="cancel">cancel</param> 64 //void NewWindow2([In, MarshalAs(UnmanagedType.IDispatch)]object pdisp, [In, Out] ref bool cancel); 65 66 /// <summary> 67 /// NewWindow3 68 /// </summary> 69 /// <param name="pdisp">pdisp</param> 70 /// <param name="cancel">cancel</param> 71 /// <param name="flags">flags</param> 72 /// <param name="urlContext">urlContext</param> 73 /// <param name="url">url</param> 74 [DispId(0x111)] 75 void NewWindow3([In, MarshalAs(UnmanagedType.IDispatch)] object pdisp, [In, Out] ref bool cancel, [In] ref object flags, [In] ref object urlContext, [In] ref object url); 76 } 77 78 /// <summary> 79 /// 创建关联性 80 /// </summary> 81 protected override void CreateSink() 82 { 83 base.CreateSink(); 84 this.events = new WebBrowserExtendedEvents(this); 85 this.cookie = new AxHost.ConnectionPointCookie(ActiveXInstance, this.events, typeof(IDWebBrowserEvents2)); 86 } 87 88 /// <summary> 89 /// 处理客户端 90 /// </summary> 91 protected override void DetachSink() 92 { 93 if (this.cookie != null) 94 { 95 this.cookie.Disconnect(); 96 this.cookie = null; 97 } 98 99 base.DetachSink(); 100 } 101 102 /// <summary> 103 /// 重写释放方法 104 /// </summary> 105 /// <param name="disposing">释放释放</param> 106 protected override void Dispose(bool disposing) 107 { 108 if (disposing && (this.components != null)) 109 { 110 this.components.Dispose(); 111 } 112 113 base.Dispose(disposing); 114 } 115 116 /// <summary> 117 /// 在打开目标页面前处理 118 /// </summary> 119 /// <param name="url">url</param> 120 /// <param name="frame">frame</param> 121 /// <param name="cancel">cancel</param> 122 protected void OnBeforeNavigate(string url, string frame, out bool cancel, object ppdisp) 123 { 124 EventHandler<WebBrowserExtendedNavigatingEventArgs> beforeNavigate = this.BeforeNavigate; 125 WebBrowserExtendedNavigatingEventArgs e = new WebBrowserExtendedNavigatingEventArgs(ppdisp, url, frame); 126 if (beforeNavigate != null) 127 { 128 beforeNavigate(this, e); 129 } 130 131 cancel = e.Cancel; 132 } 133 134 /// <summary> 135 /// 打开新窗口之前处理 136 /// </summary> 137 /// <param name="url">url</param> 138 /// <param name="cancel">释放取消</param> 139 protected void OnBeforeNewWindow(string url, out bool cancel, object ppdisp) 140 { 141 EventHandler<WebBrowserExtendedNavigatingEventArgs> beforeNewWindow = this.BeforeNewWindow; 142 WebBrowserExtendedNavigatingEventArgs e = new WebBrowserExtendedNavigatingEventArgs(ppdisp, url, null); 143 if (beforeNewWindow != null) 144 { 145 beforeNewWindow(this, e); 146 } 147 148 cancel = e.Cancel; 149 } 150 151 /// <summary> 152 /// 初始化 153 /// </summary> 154 private void InitializeComponent() 155 { 156 this.components = new Container(); 157 } 158 159 /// <summary> 160 /// WebBrowserExtendedEvents类 161 /// </summary> 162 protected class WebBrowserExtendedEvents : StandardOleMarshalObject, ExtendedWebBrowser.IDWebBrowserEvents2 163 { 164 /// <summary> 165 /// browser 166 /// </summary> 167 private ExtendedWebBrowser browser; 168 169 /// <summary> 170 /// 构造方法 171 /// </summary> 172 /// <param name="browserParam">browserParam</param> 173 public WebBrowserExtendedEvents(ExtendedWebBrowser browserParam) 174 { 175 this.browser = browserParam; 176 } 177 178 /// <summary> 179 /// BeforeNavigate2 180 /// </summary> 181 /// <param name="pdisp">pdisp</param> 182 /// <param name="url">url</param> 183 /// <param name="flags">flags</param> 184 /// <param name="targetFrameName">targetFrameName</param> 185 /// <param name="postData">postData</param> 186 /// <param name="headers">headers</param> 187 /// <param name="cancel">cancel</param> 188 public void BeforeNavigate2(object pdisp, ref object url, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel) 189 { 190 this.browser.OnBeforeNavigate((string)url, (string)targetFrameName, out cancel, pdisp); 191 } 192 193 /// <summary> 194 /// NewWindow3 195 /// </summary> 196 /// <param name="pdisp">pdisp</param> 197 /// <param name="cancel">cancel</param> 198 /// <param name="flags">flags</param> 199 /// <param name="urlContext">urlContext</param> 200 /// <param name="url">url</param> 201 public void NewWindow3(object pdisp, ref bool cancel, ref object flags, ref object urlContext, ref object url) 202 { 203 this.browser.OnBeforeNewWindow((string)url, out cancel, pdisp); 204 }
不是太理解问题。
重新整理下问题:
1、目前在学习使用WebBrowser控件的扩展类ExtendedWebBrowser做浏览器
2、在弹出新页面时,设置了e.cancel=true,即只在一个webbrowser中打开
3、要浏览的目标页面上存在window.opener或window.parent等JS
4、使用两个webbrowser打开时,无法将这两个webbrowser关联起来,所以window.opener执行时会报错。若正确关联的话,应该是能够正常执行的。
问题是:ExtendedWebBrowser如何是两个webbrowser关联起来?
不知道这样问题有没有陈述清楚呀?
不用opener呗,何必一棵树上吊死
不是我用opner,而是页面上有这个功能,需要支持。
你都已经使用WINFORM了,可以把这个Opener保存在自己定义的内存链表里面的。需要的时候问问Winform让Winform给好了。
大叔再看下整理后的问题呢~
参考
http://www.codeproject.com/csharp/ExtendedWebBrowser.asp
已解决此问题。
遇到同样的问题,请教解决方法。http://www.codeproject.com/csharp/ExtendedWebBrowser.asp这个面页已失效。求告知,谢谢