BrowserDAO.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Collections;
  6. using System.Reflection;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Drawing.Imaging;
  10. using System.Security.Permissions;
  11. using System.Runtime.InteropServices;
  12. using System.Text.RegularExpressions;
  13. namespace US.Browser.IE
  14. {
  15. /// <summary>
  16. /// webbrower统一使用接口区域
  17. /// </summary>
  18. [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  19. [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  20. public class BrowserDAO
  21. {
  22. public WebBrowser _UWB;//头部操作的WebBrowser
  23. public List<WebBrowser> _UBL = new List<WebBrowser>();//所有的WebBrowser
  24. public USMain _UF;//使用域的控件区域
  25. public FVCBrower _UFV; public RespondWeb _UVB;
  26. public Respond_setting RS;
  27. public Popup Popup;
  28. #region 创建头部窗体
  29. /// <summary>
  30. /// 带参数构造
  31. /// </summary>
  32. /// <param name="UWB"></param>
  33. /// <param name="UF"></param>
  34. public BrowserDAO(WebBrowser UWB, USMain UF)
  35. {
  36. _UF = UF; _UWB = UWB;
  37. this._UFV = new FVCBrower(this);//初始化收藏夹
  38. this._UVB = new RespondWeb(this, _UFV);//功能设置
  39. this.RS = new Respond_setting();//设置区域
  40. }
  41. /// <summary>
  42. /// 新建一个页面
  43. /// </summary>
  44. /// <param name="UURL"></param>
  45. /// <param name="UN"></param>
  46. public WebBrowser AddBrowser(string UURL, string UN)
  47. {
  48. Rectangle USZ = _UF.ClientRectangle; bool ty = _UWB.Location.Y != -35, ws = _UF.WindowState != FormWindowState.Maximized;
  49. Size SZ = ty ? ws ? new Size(USZ.Width - 6, USZ.Height - _UWB.Height - 6) : new Size(USZ.Width, USZ.Height - _UWB.Height) : new Size(USZ.Width, USZ.Height - 2);
  50. Point PT = ty ? ws ? new Point(3, _UWB.Height + 3) : PT = new Point(0, _UWB.Height) : new Point(0, 2);
  51. WebBrowser _t = new BrowserListener(UN, PT, SZ, UURL, this)._UWB; _UF.Controls.Add(_t); _UBL.Add(_t); return _t;//设置窗口 //添加窗口
  52. }
  53. #endregion
  54. #region 头部功能区域
  55. /// <summary>
  56. /// 关闭一个页面
  57. /// </summary>
  58. /// <param name="name"></param>
  59. public void CloseOneBrowser(string UN)
  60. {
  61. BLL.USIE.setTimeout(1, delegate(System.Timers.ElapsedEventArgs e)
  62. {
  63. try
  64. {
  65. int i; WebBrowser _UWB;
  66. for (i = 0; i < _UBL.Count; i++)
  67. {
  68. _UWB = _UBL[i];
  69. if (_UWB.Name == UN) { _UBL.Remove(_UWB); _UWB.Dispose(); break; }
  70. }
  71. BLL.WindowHWND.freem();
  72. }
  73. catch (Exception ex) { }
  74. });
  75. return;
  76. }
  77. /// <summary>
  78. /// 关闭全部页面
  79. /// </summary>
  80. public void CloseAllBrowser()
  81. {
  82. int i; WebBrowser _UWB;
  83. for (i = 0; i < _UBL.Count; i++)
  84. {
  85. _UWB = _UBL[i]; _UWB.Dispose(); _UBL.Remove(_UWB);
  86. }
  87. BLL.WindowHWND.freem();
  88. }
  89. /// <summary>
  90. /// 获取指定的webbrower
  91. /// </summary>
  92. /// <param name="name"></param>
  93. /// <returns></returns>
  94. public WebBrowser FindBrowser(string UN)
  95. {
  96. int i; WebBrowser _UWB = null;
  97. for (i = 0; i < _UBL.Count; i++)
  98. {
  99. _UWB = _UBL[i];
  100. if (_UWB.Name == UN) { break; }
  101. _UWB = null;
  102. }
  103. return _UWB;
  104. }
  105. /// <summary>
  106. /// 根据Url获取webbrower
  107. /// </summary>
  108. /// <param name="UN"></param>
  109. /// <returns></returns>
  110. public WebBrowser FindWebUrl(string UN)
  111. {
  112. for (int i = 0; i < _UBL.Count; i++) { if (_UBL[i].Url.AbsoluteUri.IndexOf("http://www.1473.cn") > -1) { return _UBL[i]; } }
  113. return null;
  114. }
  115. /// <summary>
  116. /// 跳转指定的页面
  117. /// </summary>
  118. /// <param name="name"></param>
  119. /// <param name="url"></param>
  120. public void ModifyOneBrowser(string UN, string UURL)
  121. {
  122. WebBrowser _UWB = this.FindBrowser(UN); BLL.WindowHWND.freem();
  123. if (_UWB != null) { if (_UWB.Document == null) { CloseOneBrowser(UN); return; } _UWB.Url = new Uri(UURL); }// _UWB.Navigate(UURL);
  124. }
  125. /// <summary>
  126. /// 获取加载页面信息信息
  127. /// </summary>
  128. /// <param name="name"></param>
  129. /// <returns></returns>
  130. public string BrowserToJson(WebBrowser _UWB, bool UE)
  131. {
  132. int i; string _UST = "", _UY = _UWB.Url.Scheme + "://" + _UWB.Url.Host; HtmlElementCollection UDE = _UWB.Document.GetElementsByTagName("link");
  133. if (UE == false)//需要获取ico
  134. {
  135. for (i = 0; i < UDE.Count; i++) { if (UDE[i].GetAttribute("rel").ToLower() == "shortcut icon") { _UST = UDE[i].GetAttribute("href").ToString(); break; } } new Uri(_UST, UriKind.RelativeOrAbsolute);
  136. if (_UST == "") { _UST = _UWB.Url.Scheme + "://" + _UWB.Url.Host + (_UST == "" ? "/favicon.ico" : _UST); } else { _UST = new Uri(new Uri(_UY), _UST).ToString(); }
  137. MemoryStream _UMS = GetUrlImg(_UST, ImageFormat.Png); _UST = _UMS == null ? "" : "data:image/jpeg;base64," + Convert.ToBase64String(_UMS.GetBuffer()); if (_UMS != null) { _UMS.Close(); _UMS.Dispose(); }
  138. }
  139. List<string[]> _T = new List<string[]> { new string[] { "Name", _UWB.Name }, new string[] { "Shortcut_Icon", _UST }, new string[] { "Url", _UWB.Url == null ? "" : _UWB.Url.ToString() }, new string[] { "Visible", _UWB.Visible.ToString() }, new string[] { "Title", (_UWB.DocumentTitle == "" && _UWB.StatusText != "完成") ? _UWB.StatusText.ToString() : _UWB.DocumentTitle }, new string[] { "CanPrevious", _UWB.CanGoBack.ToString() }, new string[] { "CanNext", _UWB.CanGoForward.ToString() } };
  140. Utitiy.StringJoiner _S = new Utitiy.StringJoiner(); _S = "{"; for (i = 0; i < _T.Count; i++) { _S += "'"; _S += _T[i][0]; _S += "':'"; _S += _T[i][1]; if (i != _T.Count - 1) { _S += "',"; } } _S += "'}"; return _S;
  141. }
  142. /// <summary>
  143. /// ico转化成png
  144. /// </summary>
  145. /// <returns></returns>
  146. public static MemoryStream GetUrlImg(string img, ImageFormat tp)
  147. {
  148. try
  149. {
  150. System.Net.HttpWebRequest webrq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(img));
  151. webrq.Timeout = 5000; webrq.ServicePoint.Expect100Continue = false;
  152. System.Net.HttpWebResponse webres = (System.Net.HttpWebResponse)webrq.GetResponse(); Bitmap imt;
  153. if (webres.StatusCode == System.Net.HttpStatusCode.OK && webres.ContentLength > 0)
  154. {
  155. using (Stream stream = webres.GetResponseStream())
  156. {
  157. Image image = Image.FromStream(stream);
  158. using (imt = new Bitmap(16, 16))
  159. {
  160. Graphics g = Graphics.FromImage(imt); g.Clear(System.Drawing.Color.Transparent);
  161. g.DrawImage(image, new System.Drawing.Rectangle(0, 0, 16, 16), new System.Drawing.Rectangle(0, 0, 16, 16), System.Drawing.GraphicsUnit.Pixel);
  162. MemoryStream ms = new MemoryStream(); imt.Save(ms, tp); imt.Dispose(); image.Dispose(); g.Dispose(); return ms;
  163. }
  164. }
  165. }
  166. webrq.Abort(); webres.Close();
  167. }
  168. catch (Exception e) { }//输出的页面信息
  169. return null;
  170. }
  171. /// <summary>
  172. /// 设置显示的webbower变化
  173. /// </summary>
  174. public void ChangeBrowsersSize()
  175. {
  176. int i; WebBrowser _UWB;
  177. for (i = 0; i < _UBL.Count; i++)
  178. {
  179. _UWB = _UBL[i]; _UWB.Location = new Point(2, this._UWB.Height); _UWB.Width = this._UF.ClientRectangle.Width - 4; _UWB.Height = this._UF.ClientRectangle.Height - this._UWB.Height - 2;
  180. }
  181. }
  182. private FormWindowState _st;
  183. private bool F11TF = false;
  184. /// <summary>
  185. /// 统一F11
  186. /// </summary>
  187. /// <param name="TF"></param>
  188. /// <param name="TopWB"></param>
  189. public void TYUWBF11(WebBrowser TopWB)
  190. {
  191. F11TF = !F11TF;
  192. if (F11TF)//全屏
  193. {
  194. _st = _UF.WindowState; _UF.WindowState = FormWindowState.Maximized;//设置全屏
  195. _UVB.BSSZ(new Point(0, 2), new Point(0, -35), new Size(_UF.Width, _UF.Height - 2), new Size(_UF.Width, TopWB.Height));//大小设置
  196. }
  197. else { _UF.WindowState = _st; _UVB.DXBH(_st == FormWindowState.Maximized); }
  198. }
  199. #endregion
  200. #region 无用函数区域
  201. ///// <summary>
  202. ///// 自定义计时器
  203. ///// </summary>
  204. ///// <param name="interval"></param>
  205. ///// <param name="action"></param>
  206. //public void SetTimeout()
  207. //{
  208. // System.Timers.Timer _TM = new System.Timers.Timer();
  209. // int i = 0;//计时器变量
  210. // _TM.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object sender, System.Timers.ElapsedEventArgs e)
  211. // {
  212. // i++;
  213. // /* if (_TM.AutoReset == false && i % 1 == 0) { Popup.UTC(); _TM.AutoReset = true; _TM.Interval = 1000; }
  214. // else if (i % 1800 == 0 && Popup.Visible == false) { Popup.UTC(); }
  215. // else if (i % 300 == 0) { Popup.REUTC(); }*/
  216. // });
  217. // //计时器执行时间区 _TM.Enabled = false; /*设置执行函数 例如: a() */
  218. // _TM.Interval = 5000;//时间间隔
  219. // _TM.Start();//启动事件
  220. // //_TM.Enabled = true;//添加这个事件设置计时器是否可用
  221. // _TM.AutoReset = false;
  222. //}
  223. #endregion
  224. }
  225. }