frmIE.cs
上传用户:hxjiliang
上传日期:2022-04-23
资源大小:182k
文件大小:20k
源码类别:

浏览器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace MyWebIE
  10. {
  11.     public partial class frmIE : Form
  12.     {
  13.         //每页标题显示字符数
  14.         private int TITLE_COUNT = 8;
  15.         public frmIE()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         #region 工具栏
  20.         /// <summary>
  21.         /// 后退按钮
  22.         /// </summary>
  23.         /// <param name="sender"></param>
  24.         /// <param name="e"></param>
  25.         private void btngoback_Click(object sender, EventArgs e)
  26.         {
  27.             getCurrentBrowser().GoBack();
  28.             setStatusButton();
  29.         }
  30.         /// <summary>
  31.         /// 前进按钮
  32.         /// </summary>
  33.         /// <param name="sender"></param>
  34.         /// <param name="e"></param>
  35.         private void btnforword_Click(object sender, EventArgs e)
  36.         {
  37.             getCurrentBrowser().GoForward();
  38.             setStatusButton();
  39.         }
  40.         /// <summary>
  41.         /// 停止按钮
  42.         /// </summary>
  43.         /// <param name="sender"></param>
  44.         /// <param name="e"></param>
  45.         private void btnStop_Click(object sender, EventArgs e)
  46.         {
  47.             getCurrentBrowser().Stop();
  48.         }
  49.         /// <summary>
  50.         /// 刷新按钮
  51.         /// </summary>
  52.         /// <param name="sender"></param>
  53.         /// <param name="e"></param>
  54.         private void btnrefresh_Click(object sender, EventArgs e)
  55.         {
  56.             getCurrentBrowser().Refresh();
  57.         }
  58.         /// <summary>
  59.         /// 主页按钮
  60.         /// </summary>
  61.         /// <param name="sender"></param>
  62.         /// <param name="e"></param>
  63.         private void btnhome_Click(object sender, EventArgs e)
  64.         {
  65.             getCurrentBrowser().GoHome();
  66.         }
  67.         /// <summary>
  68.         /// 搜索按钮
  69.         /// </summary>
  70.         /// <param name="sender"></param>
  71.         /// <param name="e"></param>
  72.         private void btnsearch_Click(object sender, EventArgs e)
  73.         {
  74.             getCurrentBrowser().GoSearch();
  75.         }
  76.         /// <summary>
  77.         /// 打印按钮
  78.         /// </summary>
  79.         /// <param name="sender"></param>
  80.         /// <param name="e"></param>
  81.         private void btnprint_Click(object sender, EventArgs e)
  82.         {
  83.             getCurrentBrowser().ShowPrintDialog();
  84.         }
  85.         /// <summary>
  86.         /// 新建按钮
  87.         /// </summary>
  88.         /// <param name="sender"></param>
  89.         /// <param name="e"></param>
  90.         private void btnNew_Click(object sender, EventArgs e)
  91.         {
  92.             newPage();
  93.         }
  94.         /// <summary>
  95.         /// 转到按钮
  96.         /// </summary>
  97.         /// <param name="sender"></param>
  98.         /// <param name="e"></param>
  99.         private void toolStripButton1_Click(object sender, EventArgs e)
  100.         {
  101.             newCurrentPageUrl(tscburl.Text);
  102.         }
  103.         /// <summary>
  104.         /// 使地址栏中的地址处于选择状态
  105.         /// </summary>
  106.         /// <param name="sender"></param>
  107.         /// <param name="e"></param>
  108.         private void toolStripLabel1_Click(object sender, EventArgs e)
  109.         {
  110.             tscburl.Select();
  111.         }
  112.         #endregion
  113.         #region 常用方法
  114.         /// <summary>
  115.         /// 设置前进、后退按钮状态
  116.         /// </summary>
  117.         private void setStatusButton()
  118.         {
  119.             btngoback.Enabled = getCurrentBrowser().CanGoBack;
  120.             btnforword.Enabled = getCurrentBrowser().CanGoForward;
  121.         }
  122.         /// <summary>
  123.         /// 当在地址栏中输入网址之后回车打开网址
  124.         /// </summary>
  125.         /// <param name="sender"></param>
  126.         /// <param name="e"></param>
  127.         private void tscburl_KeyDown(object sender, KeyEventArgs e)
  128.         {
  129.             if (e.KeyCode == Keys.Enter)
  130.             {
  131.                 newCurrentPageUrl(tscburl.Text);
  132.             }
  133.         }
  134.         /// <summary>
  135.         /// 新建一个空白页面
  136.         /// </summary>
  137.         private void newPage()
  138.         {
  139.             tscburl.Text= "about:blank";
  140.             TabPage mypage = new TabPage();
  141.             WebBrowser tempBrowser = new WebBrowser();
  142.             tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
  143.             tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);
  144.             tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
  145.             tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
  146.             tempBrowser.Dock = DockStyle.Fill;
  147.             mypage.Controls.Add(tempBrowser);
  148.             tabControl1.TabPages.Add(mypage);
  149.             tabControl1.SelectedTab = mypage;
  150.         }
  151.         /// <summary>
  152.         /// 控制进度条变化
  153.         /// </summary>
  154.         /// <param name="sender"></param>
  155.         /// <param name="e"></param>
  156.         void tempBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
  157.         {
  158.             toolStripProgressBar1.Visible = true;
  159.             toolStripProgressBar1.Maximum = (int)e.MaximumProgress;
  160.             toolStripProgressBar1.Value = (int)e.CurrentProgress;
  161.             toolStripProgressBar1.Visible = false;
  162.             setStatusButton();
  163.         }
  164.         /// <summary>
  165.         /// 新建一个页面并打开指定的网址
  166.         /// </summary>
  167.         /// <param name="address"></param>
  168.         private void newPage(string address)
  169.         {
  170.             TabPage mypage = new TabPage();
  171.             WebBrowser tempBrowser = new WebBrowser();
  172.             tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
  173.             tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);
  174.             tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
  175.             tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
  176.             tempBrowser.Url = getUrl(address);
  177.             tempBrowser.Dock = DockStyle.Fill;
  178.             mypage.Controls.Add(tempBrowser);
  179.             tabControl1.TabPages.Add(mypage);
  180.         }
  181.         /// <summary>
  182.         /// 获取当前的浏览器
  183.         /// </summary>
  184.         /// <returns></returns>
  185.         private WebBrowser getCurrentBrowser()
  186.         {
  187.             WebBrowser currentBrowser = (WebBrowser)tabControl1.SelectedTab.Controls[0];
  188.             return currentBrowser;
  189.         }
  190.         /// <summary>
  191.         /// 处理地址栏中的字符串
  192.         /// </summary>
  193.         /// <param name="address"></param>
  194.         /// <returns></returns>
  195.         private Uri getUrl(string address)
  196.         {
  197.             string tempaddress = address;
  198.             if ((!address.StartsWith("http://")) && (!address.StartsWith("https://")) && (!address.StartsWith("ftp://")))
  199.             {
  200.                 tempaddress = "http://" + address;
  201.             }
  202.             Uri myurl;
  203.             try
  204.             {
  205.                 myurl = new Uri(tempaddress);
  206.             }
  207.             catch
  208.             {
  209.                 myurl = new Uri("about:blank");
  210.             }
  211.             return myurl;
  212.         }
  213.         /// <summary>
  214.         /// 截取字符串为指定长度
  215.         /// </summary>
  216.         /// <param name="oldstring"></param>
  217.         /// <returns></returns>
  218.         private string newstring(string oldstring)
  219.         {
  220.             string temp;
  221.             if (oldstring.Length < TITLE_COUNT)
  222.             {
  223.                 temp = oldstring;
  224.             }
  225.             else
  226.             {
  227.                 temp = oldstring.Substring(0, TITLE_COUNT);
  228.             }
  229.             return temp;
  230.         }
  231.         /// <summary>
  232.         /// 将浏览过的网址写入记事本中
  233.         /// </summary>
  234.         /// <param name="s"></param>
  235.         public void WriteText(string s)
  236.         {
  237.             string strg = Application.StartupPath.ToString();
  238.             strg = strg.Substring(0, strg.LastIndexOf("\"));
  239.             strg = strg.Substring(0, strg.LastIndexOf("\"));
  240.             strg += @"UrlTxt.txt";
  241.             System.IO.StreamWriter sw = new StreamWriter(strg, true);
  242.             sw.WriteLine(s);
  243.             sw.Close();
  244.         }
  245.         /// <summary>
  246.         /// 读取记事本中的文件
  247.         /// </summary>
  248.         public void ReadText()
  249.         {
  250.             string strg = Application.StartupPath.ToString();
  251.             strg = strg.Substring(0, strg.LastIndexOf("\"));
  252.             strg = strg.Substring(0, strg.LastIndexOf("\"));
  253.             strg += @"UrlTxt.txt";
  254.             try
  255.             {
  256.                 StreamReader sr = new StreamReader(strg);
  257.                 while (sr.ReadLine() != null)
  258.                 {
  259.                     tscburl.Items.Add(sr.ReadLine().ToString());
  260.                 }
  261.                 sr.Close();
  262.             }
  263.             catch
  264.             { }
  265.         }
  266.         #endregion
  267.         #region 临时浏览器的事件
  268.         /// <summary>
  269.         /// 临时浏览器状态变化事件
  270.         /// </summary>
  271.         /// <param name="sender"></param>
  272.         /// <param name="e"></param>
  273.         void tempBrowser_StatusTextChanged(object sender, EventArgs e)
  274.         {
  275.             WebBrowser myBrowser = (WebBrowser)sender;
  276.             if (myBrowser != getCurrentBrowser())
  277.             {
  278.                 return;
  279.             }
  280.             else
  281.             {
  282.                 toolStripStatusLabel1.Text = myBrowser.StatusText;
  283.             }
  284.         }
  285.         /// <summary>
  286.         /// 在当前页面上重新定向
  287.         /// </summary>
  288.         /// <param name="address">url</param>
  289.         private void newCurrentPageUrl(String address)
  290.         {
  291.             getCurrentBrowser().Navigate(getUrl(address));
  292.         }
  293.         /// <summary>
  294.         /// 临时浏览器产生新窗体事件
  295.         /// </summary>
  296.         /// <param name="sender"></param>
  297.         /// <param name="e"></param>
  298.         void tempBrowser_NewWindow(object sender, CancelEventArgs e)
  299.         {
  300.             //获取触发tempBrowser_NewWindow事件的浏览器
  301.             WebBrowser myBrowser = (WebBrowser)sender;
  302.             //获取触发tempBrowser_NewWindow事件的浏览器所在TabPage
  303.             TabPage mypage = (TabPage)myBrowser.Parent;
  304.             //通过StatusText属性获得新的url
  305.             string NewURL = ((WebBrowser)sender).StatusText;
  306.             //生成新的一页
  307.             TabPage TabPageTemp = new TabPage();
  308.             //生成新的tempBrowser
  309.             WebBrowser tempBrowser = new WebBrowser();
  310.             //临时浏览器定向到新的url
  311.             tempBrowser.Navigate(NewURL);
  312.             tempBrowser.Dock = DockStyle.Fill;
  313.             //为临时浏览器关联NewWindow等事件
  314.             tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);
  315.             tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
  316.             tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
  317.             tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
  318.             //将临时浏览器添加到临时TabPage中
  319.             TabPageTemp.Controls.Add(tempBrowser);
  320.             //将临时TabPage添加到主窗体中
  321.             this.tabControl1.TabPages.Add(TabPageTemp);
  322.             //使外部无法捕获此事件
  323.             e.Cancel = true;
  324.         }
  325.         /// <summary>
  326.         /// 临时浏览器定向完毕
  327.         /// </summary>
  328.         /// <param name="sender"></param>
  329.         /// <param name="e"></param>
  330.         private void tempBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  331.         {
  332.             tscburl.Text = getCurrentBrowser().Url.ToString();
  333.             WebBrowser mybrowser = (WebBrowser)sender;
  334.             TabPage mypage = (TabPage)mybrowser.Parent;
  335.             //设置临时浏览器所在tab标题
  336.             mypage.Text = newstring(mybrowser.DocumentTitle);
  337.         }
  338.         #endregion
  339.         #region tabControl1控件的常用事件
  340.         /// <summary>
  341.         /// 切换不同的tab事出发此事件
  342.         /// </summary>
  343.         /// <param name="sender"></param>
  344.         /// <param name="e"></param>
  345.         private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
  346.         {
  347.             WebBrowser mybor = (WebBrowser)tabControl1.SelectedTab.Controls[0];
  348.             if (mybor.Url != null)
  349.             {
  350.                 //地址输入框
  351.                 tscburl.Text = mybor.Url.ToString();
  352.                 tabControl1.SelectedTab.Text = newstring(mybor.DocumentTitle);
  353.             }
  354.             else
  355.             {
  356.                 tscburl.Text = "about:blank";
  357.                 tabControl1.SelectedTab.Text = "空白页";
  358.             }
  359.             setStatusButton();
  360.         }
  361.         /// <summary>
  362.         /// 关闭指定的tab
  363.         /// </summary>
  364.         /// <param name="sender"></param>
  365.         /// <param name="e"></param>
  366.         private void tabControl1_DoubleClick(object sender, EventArgs e)
  367.         {
  368.             //仅仅剩下一个tab时返回
  369.             if (tabControl1.TabPages.Count <= 1)
  370.             {
  371.                 tabControl1.SelectedTab.Text = "空白页";
  372.                 getCurrentBrowser().Navigate("about:blank");
  373.             }
  374.             else
  375.             {
  376.                 //先将tabControl1隐藏然后remove掉目标tab(如果不隐藏则出现闪烁,即系统自动调转到tabControl1的第一个tab然后跳会。)最后显示tabControl1。
  377.                 tabControl1.Visible = false;
  378.                 WebBrowser mybor = getCurrentBrowser();
  379.                 //释放资源
  380.                 mybor.Dispose();
  381.                 mybor.Controls.Clear();
  382.                 this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
  383.                 //重新设置当前tab
  384.                 tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabPages.Count - 1];
  385.                 tabControl1.Visible = true;
  386.             }
  387.         }
  388.         #endregion
  389.         #region 初始化当前的浏览器窗体
  390.         /// <summary>
  391.         /// 加载窗体
  392.         /// </summary>
  393.         /// <param name="sender"></param>
  394.         /// <param name="e"></param>
  395.         private void frmIE_Load(object sender, EventArgs e)
  396.         {
  397.             initMainForm();
  398.             ReadText();
  399.         }
  400.         /// <summary>
  401.         /// 初始化浏览器
  402.         /// </summary>
  403.         private void initMainForm()
  404.         {
  405.             TabPage mypage = new TabPage();
  406.             WebBrowser tempBrowser = new WebBrowser();
  407.             tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
  408.             tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);
  409.             tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
  410.             tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
  411.             tempBrowser.Dock = DockStyle.Fill;
  412.             tempBrowser.GoHome();//和新建空白页不同
  413.             mypage.Controls.Add(tempBrowser);
  414.             tabControl1.TabPages.Add(mypage);
  415.         }
  416.         #endregion
  417.         #region 菜单栏
  418.         /// <summary>
  419.         /// 新建网页
  420.         /// </summary>
  421.         /// <param name="sender"></param>
  422.         /// <param name="e"></param>
  423.         private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
  424.         {
  425.             newPage();
  426.         }
  427.         /// <summary>
  428.         /// 另存为
  429.         /// </summary>
  430.         /// <param name="sender"></param>
  431.         /// <param name="e"></param>
  432.         private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
  433.         {
  434.             getCurrentBrowser().ShowSaveAsDialog();
  435.         }
  436.         /// <summary>
  437.         /// 打印
  438.         /// </summary>
  439.         /// <param name="sender"></param>
  440.         /// <param name="e"></param>
  441.         private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
  442.         {
  443.             getCurrentBrowser().ShowPrintDialog();
  444.         }
  445.         /// <summary>
  446.         /// 打印预览
  447.         /// </summary>
  448.         /// <param name="sender"></param>
  449.         /// <param name="e"></param>
  450.         private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
  451.         {
  452.             getCurrentBrowser().ShowPrintPreviewDialog();
  453.         }
  454.         /// <summary>
  455.         /// 页面设置
  456.         /// </summary>
  457.         /// <param name="sender"></param>
  458.         /// <param name="e"></param>
  459.         private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
  460.         {
  461.             getCurrentBrowser().ShowPageSetupDialog();
  462.         }
  463.         /// <summary>
  464.         /// 属性
  465.         /// </summary>
  466.         /// <param name="sender"></param>
  467.         /// <param name="e"></param>
  468.         private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
  469.         {
  470.             getCurrentBrowser().ShowPropertiesDialog();
  471.         }
  472.         /// <summary>
  473.         /// 关闭浏览器
  474.         /// </summary>
  475.         /// <param name="sender"></param>
  476.         /// <param name="e"></param>
  477.         private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
  478.         {
  479.             Application.Exit();
  480.         }
  481.         /// <summary>
  482.         /// 停止
  483.         /// </summary>
  484.         /// <param name="sender"></param>
  485.         /// <param name="e"></param>
  486.         private void 停止PToolStripMenuItem_Click(object sender, EventArgs e)
  487.         {
  488.             getCurrentBrowser().Stop();
  489.         }
  490.         /// <summary>
  491.         /// 刷新
  492.         /// </summary>
  493.         /// <param name="sender"></param>
  494.         /// <param name="e"></param>
  495.         private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  496.         {
  497.             getCurrentBrowser().Refresh();
  498.         }
  499.         /// <summary>
  500.         /// 源文件
  501.         /// </summary>
  502.         /// <param name="sender"></param>
  503.         /// <param name="e"></param>
  504.         private void 源文件CToolStripMenuItem_Click(object sender, EventArgs e)
  505.         {
  506.             if (tscburl.Text.Trim() != "about:blank")
  507.             {
  508.                 frmSource source = new frmSource();
  509.                 source.address = tscburl.Text.Trim();
  510.                 source.ShowDialog();
  511.             }
  512.         }
  513.         /// <summary>
  514.         /// 添加到收藏夹
  515.         /// </summary>
  516.         /// <param name="sender"></param>
  517.         /// <param name="e"></param>
  518.         private void 添加到收藏夹AToolStripMenuItem_Click(object sender, EventArgs e)
  519.         {
  520.             if (tscburl.Text.Trim() == "about:blank" || tscburl.Text.Trim() == "" || !tscburl.Text.Trim().StartsWith("http://"))
  521.             { }
  522.             else
  523.             {
  524.                 frmOpenFavorites open = new frmOpenFavorites();
  525.                 open.IUrl = tscburl.Text.Trim();
  526.                 open.ITitle = getCurrentBrowser().DocumentTitle;
  527.                 open.ShowDialog();
  528.             }
  529.         }
  530.         /// <summary>
  531.         /// 整理收藏夹
  532.         /// </summary>
  533.         /// <param name="sender"></param>
  534.         /// <param name="e"></param>
  535.         private void 整理收藏夹OToolStripMenuItem_Click(object sender, EventArgs e)
  536.         {
  537.             frmNeatenFavorites neaten = new frmNeatenFavorites();
  538.             neaten.ShowDialog();
  539.         }
  540.         #endregion 
  541.     }
  542. }