BrowserListener.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Web;
  9. using System.ComponentModel;
  10. using System.Security.Permissions;
  11. using mshtml;
  12. namespace US.Browser.IE
  13. {
  14. /// <summary>
  15. /// 建立新窗口
  16. /// </summary>
  17. [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  18. [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  19. public class BrowserListener
  20. {
  21. public WebBrowser _UWB;
  22. public BrowserDAO _UBF;
  23. public RespondWeb _UWF;
  24. private bool _UTF = false;
  25. private string URL;
  26. bool _UCT = false;
  27. bool _UE = false;
  28. /// <summary>
  29. /// 无参构造
  30. /// </summary>
  31. public BrowserListener() { }
  32. /// <summary>
  33. /// 带参数构造
  34. /// </summary>
  35. /// <param name="UN">新窗口名字</param>
  36. /// <param name="UXY">位置</param>
  37. /// <param name="UWH">大小</param>
  38. /// <param name="UURL">链接地址</param>
  39. public BrowserListener(string UN, Point UXY, Size UWH, string UURL, BrowserDAO UBF)
  40. {
  41. this._UWB = new BrowerWeb(); URL = UURL; this._UWB.Focus(); this._UBF = UBF;
  42. _UWB.Name = UN; _UWB.Location = UXY; _UWB.Size = UWH; _UWB.ScriptErrorsSuppressed = true; _UWB.ObjectForScripting = new RespondWeb(_UWB, UBF);
  43. _UWB.Url = (UURL == "" ? new System.Uri(Application.StartupPath + "\\html\\blank.html") : new Uri(HttpUtility.UrlDecode(UURL, Encoding.Default)));//
  44. AddEvent((BrowerWeb)this._UWB); //添加事件
  45. }
  46. #region 事件区域
  47. /// <summary>
  48. /// 添加事件
  49. /// </summary>
  50. private void AddEvent(BrowerWeb UWB)
  51. {
  52. UWB.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(UWB_DocumentCompleted);//页面加载成功
  53. UWB.FileDownload += new EventHandler(UWB_FileDownload); //页面下载的时候触发
  54. UWB.Navigated += new WebBrowserNavigatedEventHandler(UWB_Navigated);//加载页面
  55. UWB.Navigating += new WebBrowserNavigatingEventHandler(UWB_Navigating); //开始导航
  56. UWB.ProgressChanged += new WebBrowserProgressChangedEventHandler(UWB_ProgressChanged);//下载页面进度
  57. UWB.SizeChanged += new EventHandler(UWB_SizeChanged);//大小发生变化
  58. UWB.WindowClosing += new EventHandler(UWB_WindowClosing);//页面关闭
  59. UWB.PreviewKeyDown += new PreviewKeyDownEventHandler(UWB_PreviewKeyDown);//键盘事件
  60. SHDocVw.WebBrowser _USB = (SHDocVw.WebBrowser)UWB.ActiveXInstance; //SHDocVw.WebBrowser
  61. SHDocVw.WebBrowser_V1 _UMB = (SHDocVw.WebBrowser_V1)UWB.ActiveXInstance;
  62. _USB.NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(_UWB_NewWindow3);//弹出窗口
  63. _USB.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(_USB_NavigateError);//导航错误使用
  64. }
  65. /// <summary>
  66. /// 导航变化
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. void UWB_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  71. {
  72. if (_UWB.IsBusy == false && _UWB.Url == e.Url && _UWB.ReadyState != WebBrowserReadyState.Uninitialized)
  73. {
  74. _UE = false; _UBF._UWB.Document.InvokeScript("GetJson", new string[] { _UBF.BrowserToJson(_UWB, !_UCT), _UWB.Name });
  75. }
  76. }
  77. /// <summary>
  78. /// 导航到时错误
  79. /// </summary>
  80. /// <param name="pDisp"></param>
  81. /// <param name="URL"></param>
  82. /// <param name="Frame"></param>
  83. /// <param name="StatusCode"></param>
  84. /// <param name="Cancel"></param>
  85. void _USB_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
  86. {
  87. _UE = true;
  88. }
  89. /// <summary>
  90. /// 页面关闭触发
  91. /// </summary>
  92. /// <param name="sender"></param>
  93. /// <param name="e"></param>
  94. void UWB_WindowClosing(object sender, EventArgs e)
  95. {
  96. //this._UWB.Parent.MouseWheel -= Parent_MouseWheel; //页面关闭
  97. //BrowerWeb _UWB = (BrowerWeb)sender; _UWB.WindowClosing -= UWB_WindowClosing;
  98. //_UBF._UWB.Document.InvokeScript("CloseTag", new object[] { _UWB.Name });
  99. }
  100. /// <summary>
  101. /// 统一页面加载
  102. /// </summary>
  103. /// <param name="_t"></param>
  104. private void UWB_loading(WebBrowser _UWB)
  105. {
  106. _UBF._UWB.Document.InvokeScript("GetJson", new string[] { _UBF.BrowserToJson(_UWB, _UE), _UWB.Name });
  107. }
  108. /// <summary>
  109. /// 页面加载完毕
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. void UWB_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  114. {
  115. WebBrowser _UBW = (WebBrowser)sender;
  116. if (_UBW.ReadyState == WebBrowserReadyState.Complete && e.Url == _UBW.Url)
  117. {
  118. if (_UBW.Visible == true) { _UBW.Focus(); } _UCT = true; UWB_loading(_UBW);
  119. _UBF._UWB.Document.InvokeScript("GetJson", new string[] { _UBF.BrowserToJson(_UWB, !_UCT), _UWB.Name });
  120. _UBW.Parent.MouseWheel += new MouseEventHandler(Parent_MouseWheel); AddGG(_UBW);
  121. }
  122. }
  123. /// <summary>
  124. /// 内嵌广告
  125. /// </summary>
  126. /// <param name="UW"></param>
  127. void AddGG(WebBrowser UW)
  128. {
  129. UW = UW == null ? UW : _UWB; HtmlDocument _UD = UW.Document; HtmlElement _UDOD = null;
  130. if (UW.Url.ToString().IndexOf("www.sogou.com") > -1) { _UDOD = _UD.GetElementById("kmap_entity_div"); }
  131. else if (UW.Url.ToString().IndexOf("www.baidu.com") > -1) { _UDOD = _UD.GetElementById("content_right"); }
  132. if (_UDOD != null)
  133. {
  134. HtmlElement _UDTD = _UD.CreateElement("div"); _UDTD.Style = "padding-bottom:20px;"; _UDTD.Id = Guid.NewGuid().ToString(); _UDOD.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterBegin, _UDTD);//_UDOD.AppendChild(_UDTD);
  135. HtmlElement _UDSD = _UD.CreateElement("script"); IHTMLScriptElement _USE = (IHTMLScriptElement)_UDSD.DomElement;
  136. _USE.type = "text/javascript"; _USE.text = "baidu_a = document.createElement('script'); baidu_a.onreadystatechange = function () { BAIDU_CLB_fillSlotAsync('u2391504', '" + _UDTD.Id + "'); }; baidu_a.type = 'text/javascript'; baidu_a.charset = 'gbk'; baidu_a.src = 'http://cbjs.baidu.com/js/m.js'; document.getElementsByTagName('head')[0].appendChild(baidu_a);";
  137. _UD.GetElementsByTagName("head")[0].AppendChild(_UDSD);
  138. //HtmlElement _UDTD = _UD.CreateElement("a"); _UDTD.Style = "display:none!important"; _UDTD.Id = "tanx-a-mm_32325420_3265865_39858206"; _UDOD.AppendChild(_UDTD);
  139. //HtmlElement _UDSD = _UD.CreateElement("script"); _UDSD.Id = "tanx-s-mm_32325420_3265865_39858206"; IHTMLScriptElement _USE = (IHTMLScriptElement)_UDSD.DomElement;
  140. //_USE.type = "text/javascript"; _USE.src = "http://p.tanx.com/ex?i=mm_32325420_3265865_39858206"; _UD.GetElementsByTagName("head")[0].AppendChild(_UDSD);
  141. }
  142. }
  143. void Window_Resize(object sender, HtmlElementEventArgs e)
  144. {
  145. }
  146. /// <summary>
  147. /// 滚动事件监视
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. void Parent_MouseWheel(object sender, MouseEventArgs e)
  152. {
  153. if (_UWB.Parent != null)
  154. {
  155. Point _UXY = Cursor.Position; Point _UPE = _UWB.Parent.Location;
  156. if (((!FVCBrower.UAFB.Visible && !FVCBrower.UPFB.Visible) || (!FVCBrower.UAFB.Bounds.Contains(_UXY.X - _UPE.X, _UXY.Y - _UPE.Y) && !FVCBrower.UPFB.Bounds.Contains(_UXY.X - _UPE.X, _UXY.Y - _UPE.Y))) && _UWB.Visible) { _UWB.Document.Focus(); }
  157. }
  158. }
  159. /// <summary>
  160. /// 新建窗口处理区域
  161. /// </summary>
  162. /// <param name="ppDisp"></param>
  163. /// <param name="Cancel"></param>
  164. /// <param name="dwFlags"></param>
  165. /// <param name="bstrUrlContext"></param>
  166. /// <param name="bstrUrl"></param>
  167. void _UWB_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
  168. {
  169. string _UN = "a" + Guid.NewGuid().ToString(); WebBrowser _UMB = _UBF.AddBrowser(bstrUrl, _UN); HtmlElement _UHE = this._UWB.Document.ActiveElement;
  170. bstrUrl = _UHE == null ? bstrUrl : _UHE.GetAttribute("href") == "" ? bstrUrl : _UHE.GetAttribute("href"); _UMB.Visible = true;
  171. _UBF._UWB.Document.InvokeScript("addT", new object[] { bstrUrl, _UN }); ppDisp = (_UMB.ActiveXInstance as SHDocVw.IWebBrowser).Application;
  172. // if (dwFlags == 6 || dwFlags == 134) { _UBF._UWB.Document.InvokeScript("addT", new object[] { bstrUrl, _UN }); } else if (dwFlags == 65670) { } //新窗口打开
  173. }
  174. /// <summary>
  175. /// 键盘点击事件
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. void UWB_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  180. {
  181. int _TKV = e.KeyValue;
  182. if (_UTF == true)
  183. {
  184. switch (_TKV)
  185. {
  186. case 122: _UBF.TYUWBF11((WebBrowser)US.Browser.IE.USMain._UFL[0].Controls.Find("USBrowserNav", true)[0]); break;
  187. case 123: IntPtr a = BLL.WindowHWND.GetHWND(); MessageBox.Show("暂未实现,敬请期待。欢迎光临http://www.1473.cn!"); break;
  188. default: break;
  189. }
  190. }
  191. _UTF = _UTF == false ? true : false;
  192. }
  193. /// <summary>
  194. /// 开始导航页面
  195. /// </summary>
  196. /// <param name="sender"></param>
  197. /// <param name="e"></param>
  198. void UWB_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  199. {
  200. if (e.Url == this._UWB.Url && URL != null) { this._UWB.Size += new Size(1, 0); this._UWB.Size -= new Size(1, 0); }
  201. }
  202. /// <summary>
  203. /// 大小变化
  204. /// </summary>
  205. /// <param name="sender"></param>
  206. /// <param name="e"></param>
  207. void UWB_SizeChanged(object sender, EventArgs e)
  208. {
  209. }
  210. /// <summary>
  211. /// 进度变化
  212. /// </summary>
  213. /// <param name="sender"></param>
  214. /// <param name="e"></param>
  215. void UWB_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
  216. {
  217. }
  218. /// <summary>
  219. /// 页面下载
  220. /// </summary>
  221. /// <param name="sender"></param>
  222. /// <param name="e"></param>
  223. void UWB_FileDownload(object sender, EventArgs e)
  224. {
  225. }
  226. #endregion
  227. }
  228. }