Form1.cs
上传用户:xzl658
上传日期:2022-04-13
资源大小:1766k
文件大小:17k
源码类别:

浏览器

开发平台:

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.Web;
  9. using System.Threading;
  10. using System.IO;
  11. using System.Net;
  12. namespace MyIE
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.       /*  System.Windows.Forms.WebBrowser[] webNew = new WebBrowser();*/
  17.         List<WebBrowser> webNew = new List<WebBrowser>();
  18.         private String ur1;
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             this.HomePage();//主页
  23.         }
  24.         private void toolStripButton3_Click(object sender, EventArgs e)
  25.         {
  26.             try
  27.             {
  28.                 webNew[tabControl1.SelectedIndex].Stop();//停止
  29.             }
  30.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  31.         }
  32.         //输入网址,并执行过程
  33.         private void toolStripButton9_Click(object sender, EventArgs e)
  34.         {
  35.             KeyEventArgs ev = new KeyEventArgs(Keys.Enter);
  36.             toolcmbHype_KeyDown(sender, ev);
  37.         }
  38.         private void toolcmbHype_KeyDown(object sender, KeyEventArgs e)
  39.         {
  40.          try{
  41.              if (e.KeyCode == Keys.Enter)
  42.              {
  43.                  this.ur1 = this.toolStripComboBox1.Text.Trim();
  44.                  if (ur1.StartsWith("www.") || ur1.StartsWith("WWW."))
  45.                      ur1 = "http://" + ur1 + @"/";
  46.                  if (ur1.StartsWith("http://") || ur1.StartsWith("ftp://"))
  47.                      this.toolStripComboBox1.Text = ur1;                
  48.                  
  49.                  
  50.                  if (this.tabControl1.TabPages.Count == 0)
  51.                  {
  52.                      this.webNew.Add(new WebBrowser());
  53.                      this.tabControl1.TabPages.Add("", "正在载入……", 0);
  54.                      this.tabControl1.TabPages[0].Controls.Add(webNew[tabControl1.SelectedIndex]);
  55.                      this.webNew[tabControl1.SelectedIndex].Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webNew_Navigated);
  56.                      this.webNew[tabControl1.SelectedIndex].NewWindow += new System.ComponentModel.CancelEventHandler(this.webNew_NewWindow);
  57.                      this.webNew[tabControl1.SelectedIndex].ProgressChanged += new System.Windows.Forms.WebBrowserProgressChangedEventHandler(this.webNew_ProgressChanged);
  58.                      this.webNew[tabControl1.SelectedIndex].Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webNew_Navigating);
  59.                      this.webNew[tabControl1.SelectedIndex].DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webNew_DocumentCompleted);
  60.                  }
  61.                  this.tabControl1.TabPages[tabControl1.SelectedIndex].Text ="正在载入……";
  62.                  if(this.menuStrip1.Visible == true)
  63.                  this.progressBar1.Visible = true;
  64.                  webNew[tabControl1.SelectedIndex].Dock = DockStyle.Fill;
  65.                  this.webNew[tabControl1.SelectedIndex].Navigate(ur1);
  66.                  this.toolStripButton10.Enabled = true;
  67.                  this.toolStripButton8.Enabled = true;
  68.                  bool IsAdd = false;
  69.                  for (int i = 0; i < toolStripComboBox1.Items.Count; i++)
  70.                  {
  71.                      if (this.ur1.Equals((String)toolStripComboBox1.Items[i]))
  72.                      { IsAdd = true; break; }
  73.                  }
  74.                  if (!IsAdd)
  75.                  {
  76.                      toolStripComboBox1.Items.Add(this.ur1);
  77.                  }
  78.              }
  79.         }
  80.         catch (Exception ee) { MessageBox.Show(ee.Message); }
  81.         }
  82.         private void webNew_NewWindow(object sender, CancelEventArgs e)
  83.         {
  84.             try
  85.             {
  86.                 e.Cancel = true;
  87.                 this.tabControl1.TabPages.Add("", "正在载入……", 0);
  88.                 this.tabControl1.SelectedIndex = this.tabControl1.TabPages.Count - 1;
  89.                 this.webNew.Add(new WebBrowser());
  90.                 this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Add(webNew[tabControl1.SelectedIndex]);
  91.                 webNew[tabControl1.SelectedIndex].Dock = DockStyle.Fill;
  92.                 if (this.menuStrip1.Visible == true)
  93.                 this.progressBar1.Visible = true;
  94.                 WebBrowser srcBrowser = (WebBrowser)sender;
  95.                 string newUrl = srcBrowser.StatusText;
  96.                 this.toolStripComboBox1.Text = newUrl;               
  97.                 webNew[tabControl1.SelectedIndex].Navigate(newUrl);
  98.                // this.toolStripButton10.Enabled = true;
  99.                // this.toolStripButton8.Enabled = true;
  100.                 this.webNew[tabControl1.SelectedIndex].Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webNew_Navigated);
  101.                 this.webNew[tabControl1.SelectedIndex].NewWindow += new System.ComponentModel.CancelEventHandler(this.webNew_NewWindow);
  102.                 this.webNew[tabControl1.SelectedIndex].ProgressChanged += new System.Windows.Forms.WebBrowserProgressChangedEventHandler(this.webNew_ProgressChanged);
  103.                 this.webNew[tabControl1.SelectedIndex].Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webNew_Navigating);
  104.                 this.webNew[tabControl1.SelectedIndex].DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webNew_DocumentCompleted);                
  105.             
  106.             }  
  107.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  108.         }
  109.         private void webNew_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
  110.         {
  111.             progressBar1.Maximum = (int)e.MaximumProgress;
  112.             progressBar1.Value = (int)e.CurrentProgress;
  113.         }
  114.         private void webNew_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  115.         {
  116.         }
  117.         private void webNew_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  118.         {
  119.             toolStripStatusLabel2.Text = e.Url.ToString();
  120.         }
  121.         private void Form1_Resize(object sender, EventArgs e)
  122.         {
  123.             this.toolStripComboBox1.Width = this.statusStrip1.Width - 200;
  124.         }
  125.         private void toolStripButton5_Click(object sender, EventArgs e)
  126.         {
  127.             this.HomePage();
  128.         }
  129.         private void HomePage()
  130.         {
  131.             try
  132.             {
  133.                 KeyEventArgs ev = new KeyEventArgs(Keys.Enter);
  134.                 object sender = new object();
  135.                 toolcmbHype_KeyDown(sender, ev);
  136.                 webNew[tabControl1.SelectedIndex].Navigate("http://www.baidu.com/");
  137.                 this.toolStripComboBox1.Text = "http://www.baidu.com/";
  138.             }
  139.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  140.         }
  141.         private void toolStripButton4_Click(object sender, EventArgs e)
  142.         {
  143.             try
  144.             {
  145.                 webNew[tabControl1.SelectedIndex].Refresh();
  146.             }
  147.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  148.         }
  149.         private void tabControl1_Click(object sender, EventArgs e)
  150.         {
  151.             this.toolStripComboBox1.Text = this.webNew[tabControl1.SelectedIndex].Url.ToString();
  152.             string a = this.webNew[tabControl1.SelectedIndex].DocumentTitle + ("…………………………");
  153.             this.tabControl1.TabPages[tabControl1.SelectedIndex].Text = a.Substring(0, 6);
  154.             PanDuan();
  155.         }
  156.         private void toolStripButton1_Click(object sender, EventArgs e)
  157.         {
  158.             try
  159.             {
  160.                 this.webNew[tabControl1.SelectedIndex].GoBack();
  161.             }
  162.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  163.         }
  164.         private void toolStripButton2_Click(object sender, EventArgs e)
  165.         {
  166.             try
  167.             {
  168.                 this.webNew[tabControl1.SelectedIndex].GoForward();
  169.             }
  170.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  171.         }
  172.         private void toolStripButton8_Click(object sender, EventArgs e)
  173.         {
  174.             try
  175.             {
  176.                 if (this.tabControl1.TabCount > 0)
  177.                 {
  178.                     webNew.Clear();
  179.                     tabControl1.TabPages.Clear();
  180.                     this.toolStripComboBox1.Text = "";
  181.                     this.toolStripButton1.Enabled = false;
  182.                     this.toolStripButton2.Enabled = false;
  183.                     this.向后BToolStripMenuItem.Enabled = false;
  184.                     this.向前FToolStripMenuItem.Enabled = false;
  185.                     this.toolStripButton10.Enabled = false;
  186.                     this.toolStripButton8.Enabled = false;
  187.                     this.toolStripStatusLabel2.Text = "";
  188.                 }
  189.             }
  190.             catch (Exception ee) { MessageBox.Show(ee.Message); }
  191.             
  192.         }
  193.         private void toolStripButton10_Click(object sender, EventArgs e)
  194.         {
  195.             try
  196.             {
  197.                 this.webNew[tabControl1.SelectedIndex].Dispose();
  198.                 this.webNew.Remove(this.webNew[tabControl1.SelectedIndex]);
  199.                 tabControl1.TabPages[tabControl1.SelectedIndex].Dispose();
  200.                 if (tabControl1.SelectedIndex >= 0)
  201.                     PanDuan();
  202.                 else 
  203.                 {
  204.                     this.toolStripComboBox1.Text = "";
  205.                     this.toolStripStatusLabel2.Text = "";
  206.                     this.toolStripButton1.Enabled = false;
  207.                     this.向前FToolStripMenuItem.Enabled = false;
  208.                     this.toolStripButton2.Enabled = false;
  209.                     this.向后BToolStripMenuItem.Enabled = false;
  210.                     this.toolStripButton10.Enabled = false;
  211.                     this.toolStripButton8.Enabled = false;
  212.                 }
  213.             }
  214.             catch (Exception ee) { MessageBox.Show(ee.Message); }          
  215.         }
  216.         
  217.         private void webNew_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  218.         {
  219.             string a = this.webNew[tabControl1.SelectedIndex].DocumentTitle + ("……………………");
  220.             this.tabControl1.TabPages[tabControl1.SelectedIndex].Text = a.Substring(0, 6);
  221.             this.toolStripComboBox1.Text = this.webNew[tabControl1.SelectedIndex].Url.ToString();
  222.             PanDuan();
  223.             this.progressBar1.Visible = false;
  224.        //   this.tabControl1.Size = new Size();
  225.         }
  226.         private void PanDuan()
  227.         {
  228.                 if (this.webNew[tabControl1.SelectedIndex].CanGoBack)
  229.             {
  230.                 this.toolStripButton1.Enabled = true;
  231.                 this.向前FToolStripMenuItem.Enabled = true;
  232.             }
  233.             else
  234.             {
  235.                 this.toolStripButton1.Enabled = false;
  236.                 this.向前FToolStripMenuItem.Enabled = false;
  237.             }
  238.             if (this.webNew[tabControl1.SelectedIndex].CanGoForward)
  239.             {
  240.                 this.toolStripButton2.Enabled = true;
  241.                 this.向后BToolStripMenuItem.Enabled = true;
  242.             }
  243.             else
  244.             {
  245.                 this.toolStripButton2.Enabled = false;
  246.                 this.向后BToolStripMenuItem.Enabled = false;
  247.             }
  248.             
  249.     }
  250.         private void 关于MyIEToolStripMenuItem_Click(object sender, EventArgs e)
  251.         {
  252.             AboutMyIE a = new AboutMyIE();
  253.             a.Show();
  254.         }
  255.         private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
  256.         {
  257.             this.openFileDialog1.ShowDialog();
  258.             string StrFileName = this.openFileDialog1.FileName;
  259.             try
  260.             {
  261.                 this.webNew[tabControl1.SelectedIndex].Navigate(StrFileName);
  262.             }
  263.             catch (Exception Error)
  264.             {
  265.                 MessageBox.Show(Error.Message);
  266.             }
  267.         }
  268.         private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
  269.         {
  270.             this.tabControl1.TabPages.Add("              ");
  271.             this.webNew.Add(new WebBrowser());
  272.             this.tabControl1.SelectedIndex = this.tabControl1.TabPages.Count - 1;
  273.             this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Add(webNew[tabControl1.SelectedIndex]);
  274.             webNew[tabControl1.SelectedIndex].Dock = DockStyle.Fill;            
  275.             webNew[tabControl1.SelectedIndex].Navigate("");
  276.             this.toolStripComboBox1.Text = "about:blank";
  277.         }
  278.         private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
  279.         {
  280.             this.webNew[tabControl1.SelectedIndex].ShowPrintPreviewDialog();
  281.         }
  282.         private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
  283.         {
  284.             this.webNew[tabControl1.SelectedIndex].ShowPageSetupDialog();
  285.         }
  286.         private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  287.         {
  288.             this.webNew[tabControl1.SelectedIndex].ShowPrintDialog();
  289.         }
  290.         private void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  291.         {
  292.         }
  293.         private void 另存为sToolStripMenuItem_Click(object sender, EventArgs e)
  294.         {
  295.             this.webNew[tabControl1.SelectedIndex].ShowSaveAsDialog();
  296.         }
  297.         private void toolStripButton7_Click(object sender, EventArgs e)
  298.         {
  299.             this.webNew[tabControl1.SelectedIndex].ShowPrintDialog();
  300.         }
  301.         private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
  302.         {
  303.             this.Close();
  304.         }
  305.         private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
  306.         {
  307.             if (this.工具栏ToolStripMenuItem.Checked)
  308.             {
  309.                 this.工具栏ToolStripMenuItem.Checked = false;
  310.                 this.toolStrip1.Hide();
  311.             }
  312.             else
  313.             {
  314.                 this.工具栏ToolStripMenuItem.Checked = true;
  315.                 this.toolStrip1.Show();
  316.             }
  317.         }
  318.         private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
  319.         {
  320.             if (this.状态栏ToolStripMenuItem.Checked)
  321.             {
  322.                 this.状态栏ToolStripMenuItem.Checked = false;
  323.                 this.statusStrip1.Hide();
  324.             }
  325.             else
  326.             {
  327.                 this.状态栏ToolStripMenuItem.Checked = true;
  328.                 this.statusStrip1.Show();
  329.             }
  330.         }
  331.         private void 页面源代码ToolStripMenuItem_Click(object sender, EventArgs e)
  332.         {
  333.             HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(webNew[tabControl1.SelectedIndex].Url);
  334.             HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
  335.             StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
  336.             string sResultContents = sr.ReadToEnd();
  337.             oResponse.Close();
  338.             Form2 a = new Form2();
  339.             a.xianshi(sResultContents);
  340.             a.Show();
  341.         }
  342.         private void toolStripMenuItem2_Click(object sender, EventArgs e)
  343.         {
  344.             this.webNew[tabControl1.SelectedIndex].Stop();
  345.         }
  346.         private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
  347.         {
  348.            
  349.         }
  350.         private void toolStripMenuItem3_Click(object sender, EventArgs e)
  351.         {
  352.             this.toolStripButton5_Click(null,null);
  353.         }
  354.         private void 向后BToolStripMenuItem_Click(object sender, EventArgs e)
  355.         {
  356.             this.toolStripButton2_Click(null,null);
  357.         }
  358.         private void 向前FToolStripMenuItem_Click(object sender, EventArgs e)
  359.         {
  360.             this.toolStripButton1_Click(null,null);
  361.         }
  362.         private void tabControl1_Selected(object sender, TabControlEventArgs e)
  363.         {
  364.         }
  365.         private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
  366.         {
  367.             webNew[tabControl1.SelectedIndex].ShowPropertiesDialog();
  368.         }
  369.         private void 全屏显示FToolStripMenuItem_Click(object sender, EventArgs e)
  370.         {
  371.             this.TopMost = true;
  372.             this.toolStrip1.Hide();
  373.             this.menuStrip1.Hide();
  374.             this.statusStrip1.Hide();
  375.             this.FormBorderStyle = FormBorderStyle.None;
  376.             this.WindowState = FormWindowState.Maximized;           
  377.             this.toolStripButton11.Visible = true;
  378.             this.progressBar1.Visible = false;
  379.             this.toolStripComboBox1.Width = this.statusStrip1.Width - 200;
  380.         }
  381.         private void toolStripButton11_Click(object sender, EventArgs e)
  382.         {
  383.             this.FormBorderStyle = FormBorderStyle.Sizable;
  384.             this.TopMost = false;
  385.             this.toolStrip1.Show();
  386.             this.menuStrip1.Show();
  387.             this.statusStrip1.Show();
  388.             this.toolStripButton11.Visible = false;            
  389.         }
  390.     }
  391. }