C#网络浏览器设计思路及源代码 第3页

C#网络浏览器设计思路及源代码 第3页
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;

namespace WebBrowser
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class MainForm : System.Windows.Forms.Form
 {
  #region Controls
  private System.Windows.Forms.Label lblAddress;
  private AxSHDocVw.AxWebBrowser mainBrowser;
  private System.Windows.Forms.Button btnGo;
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem5;
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.StatusBar mainStatus;
  private System.Windows.Forms.StatusBarPanel statusBarInfo;
  private System.Windows.Forms.ErrorProvider myErrorProvider;
  private System.Windows.Forms.MenuItem menu_File;
  private System.Windows.Forms.MenuItem menu_FileOpen;
  private System.Windows.Forms.MenuItem menu_FileSave;
  private System.Windows.Forms.MenuItem menu_FileSaveAs;
  private System.Windows.Forms.MenuItem menu_FileExit;
  private System.Windows.Forms.MenuItem menuItem16;
  private System.Windows.Forms.MenuItem menu_FileProperty;
  private System.Windows.Forms.MenuItem menu_EditCut;
  private System.Windows.Forms.MenuItem menu_EditCopy;
  private System.Windows.Forms.MenuItem menuEditPaste;
  private System.Windows.Forms.MenuItem menu_EditSelAll;
  private System.Windows.Forms.MenuItem menu_EditFind;
  private System.Windows.Forms.MenuItem menu_Edit;
  private System.Windows.Forms.MenuItem menu_View;
  private System.Windows.Forms.MenuItem menu_Tool;
  private System.Windows.Forms.MenuItem menu_Help;
  private System.Windows.Forms.MenuItem menu_HelpAbout;
  private System.Windows.Forms.MenuItem menu_ViewSource;
  private System.Windows.Forms.MenuItem menu_ToolInternet;
  private System.Windows.Forms.ToolBar mainToolBar;
  private System.Windows.Forms.TabControl mainTabCtrl;
  private System.Windows.Forms.Button btnCloseCurrent;
  private System.Windows.Forms.Button btnCloseAll;
  private System.Windows.Forms.ToolBarButton tbb_Back;
  private System.Windows.Forms.ToolBarButton tbb_Forward;
  private System.Windows.Forms.ToolBarButton tbb_Stop;
  private System.Windows.Forms.ToolBarButton tbb_Refresh;
  private System.Windows.Forms.ToolBarButton tbb_GoHome;
  private System.ComponentModel.IContainer components;
  private System.Windows.Forms.ComboBox comboAddress;
  private System.Windows.Forms.MenuItem menu_FileNew;
  private System.Windows.Forms.ToolBarButton tbb_New;
  private System.Windows.Forms.ToolBarButton toolBarButton2;
  private System.Windows.Forms.ToolBarButton toolBarButton3;
  private System.Windows.Forms.ToolBarButton toolBarButton1;
  #endregion
  private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");
  private string FormerListFile = "FormerList.dat";
  private ArrayList VisitedList = new ArrayList();

  public MainForm()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
   this.mainBrowser = new AxSHDocVw.AxWebBrowser();
   this.lblAddress = new System.Windows.Forms.Label();
   this.btnGo = new System.Windows.Forms.Button();
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.menu_File = new System.Windows.Forms.MenuItem();
   this.menu_FileOpen = new System.Windows.Forms.MenuItem();
   this.menu_FileSave = new System.Windows.Forms.MenuItem();
   this.menu_FileSaveAs = new System.Windows.Forms.MenuItem();
   this.menuItem5 = new System.Windows.Forms.MenuItem();
   this.menu_FileProperty = new System.Windows.Forms.MenuItem();
   this.menu_FileExit = new System.Windows.Forms.MenuItem();
   this.menu_Edit = new System.Windows.Forms.MenuItem();
   this.menu_EditCut = new System.Windows.Forms.MenuItem();
   this.menu_EditCopy = new System.Windows.Forms.MenuItem();
   this.menuEditPaste = new System.Windows.Forms.MenuItem();
   this.menuItem16 = new System.Windows.Forms.MenuItem();
   this.menu_EditSelAll = new System.Windows.Forms.MenuItem();
   this.menu_EditFind = new System.Windows.Forms.MenuItem();
   this.menu_View = new System.Windows.Forms.MenuItem();
   this.menu_ViewSource = new System.Windows.Forms.MenuItem();
   this.menu_Tool = new System.Windows.Forms.MenuItem();
   this.menu_ToolInternet = new System.Windows.Forms.MenuItem();
   this.menu_Help = new System.Windows.Forms.MenuItem();
   this.menu_HelpAbout = new System.Windows.Forms.MenuItem();
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   this.mainStatus = new System.Windows.Forms.StatusBar();
   this.statusBarInfo = new System.Windows.Forms.StatusBarPanel();
   this.myErrorProvider = new System.Windows.Forms.ErrorProvider();
   this.mainToolBar = new System.Windows.Forms.ToolBar();
   this.tbb_Back = new System.Windows.Forms.ToolBarButton();
   this.tbb_Forward = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
   this.tbb_Stop = new System.Windows.Forms.ToolBarButton();
   this.tbb_Refresh = new System.Windows.Forms.ToolBarButton();
   this.tbb_GoHome = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
   this.mainTabCtrl = new System.Windows.Forms.TabControl();
   this.btnCloseCurrent = new System.Windows.Forms.Button();
   this.btnCloseAll = new System.Windows.Forms.Button();
   this.comboAddress = new System.Windows.Forms.ComboBox();
   this.menu_FileNew = new System.Windows.Forms.MenuItem();
   this.tbb_New = new System.Windows.Forms.ToolBarButton();
   this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
   ((System.ComponentModel.ISupportInitialize)(this.mainBrowser)).BeginInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarInfo)).BeginInit();
   this.SuspendLayout();
   //
   // mainBrowser
   //
   this.mainBrowser.Enabled = true;
   this.mainBrowser.Location = new System.Drawing.Point(576, 104);
   this.mainBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mainBrowser.OcxState")));
   this.mainBrowser.Size = new System.Drawing.Size(16, 16);
   this.mainBrowser.TabIndex = 0;
   this.mainBrowser.Visible = false;
   this.mainBrowser.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.mainBrowser_StatusTextChange);
   this.mainBrowser.WindowClosing += new AxSHDocVw.DWebBrowserEvents2_WindowClosingEventHandler(this.mainBrowser_WindowClosing);
   this.mainBrowser.NewWindow3 += new AxSHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(this.mainBrowser_NewWindow3);
   this.mainBrowser.NavigateError += new AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(this.mainBrowser_NavigateError);
   this.mainBrowser.CommandStateChange += new AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(this.mainBrowser_CommandStateChange);
   this.mainBrowser.TitleChange += new AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(this.mainBrowser_TitleChange);
   this.mainBrowser.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.mainBrowser_NavigateComplete2);
   this.mainBrowser.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.mainBrowser_DocumentComplete);
   this.mainBrowser.ProgressChange += new AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(this.mainBrowser_ProgressChange);
   this.mainBrowser.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.mainBrowser_NewWindow2);
   this.mainBrowser.BeforeNavigate2 += new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.mainBrowser_BeforeNavigate2);
   //
   // lblAddress
   //
   this.lblAddress.Location = new System.Drawing.Point(0, 48);
   this.lblAddress.Name = "lblAddress";
   this.lblAddress.Size = new System.Drawing.Size(48, 16);
   this.lblAddress.TabIndex = 1;
   this.lblAddress.Text = "地址";
   //
   // btnGo
   //
   this.btnGo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.btnGo.Location = new System.Drawing.Point(320, 40);
   this.btnGo.Name = "btnGo";
   this.btnGo.Size = new System.Drawing.Size(75, 24);
   this.btnGo.TabIndex = 3;
   this.btnGo.Text = "转到";
   this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
   //
   // mainMenu1
   //
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_File,
                       this.menu_Edit,
                       this.menu_View,
                       this.menu_Tool,
                       this.menu_Help});
   //
   // menu_File
   //
   this.menu_File.Index = 0;
   this.menu_File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_FileNew,
                       this.menu_FileOpen,
                       this.menu_FileSave,
                       this.menu_FileSaveAs,
                       this.menuItem5,
                       this.menu_FileProperty,
                       this.menu_FileExit});
   this.menu_File.Text = "文件(&F)";
   //
   // menu_FileOpen
   //
   this.menu_FileOpen.Index = 1;
   this.menu_FileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
   this.menu_FileOpen.Text = "打开";
   this.menu_FileOpen.Click += new System.EventHandler(this.menu_FileOpen_Click);
   //
   // menu_FileSave
   //
   this.menu_FileSave.Index = 2;
   this.menu_FileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
   this.menu_FileSave.Text = "保存";
   this.menu_FileSave.Click += new System.EventHandler(this.menu_FileSave_Click);
   //
   // menu_FileSaveAs
   //
   this.menu_FileSaveAs.Index = 3;
   this.menu_FileSaveAs.Text = "另存为...";
   this.menu_FileSaveAs.Click += new System.EventHandler(this.menu_FileSaveAs_Click);
   //
   // menuItem5
   //
   this.menuItem5.Index = 4;
   this.menuItem5.Text = "-";
   //
   // menu_FileProperty
   //
   this.menu_FileProperty.Index = 5;
   this.menu_FileProperty.Text = "属性(&R)";
   this.menu_FileProperty.Click += new System.EventHandler(this.menu_FileProperty_Click);
   //
   // menu_FileExit
   //
   this.menu_FileExit.Index = 6;
   this.menu_FileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
   this.menu_FileExit.Text = "退出(&C)";
   this.menu_FileExit.Click += new System.EventHandler(this.menu_FileExit_Click);
   //
   // menu_Edit
   //
   this.menu_Edit.Index = 1;
   this.menu_Edit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_EditCut,
                       this.menu_EditCopy,
                       this.menuEditPaste,
                       this.menuItem16,
                       this.menu_EditSelAll,
                       this.menu_EditFind});
   this.menu_Edit.Text = "编辑(&E)";
   //
   // menu_EditCut
   //
   this.menu_EditCut.Index = 0;
   this.menu_EditCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
   this.menu_EditCut.Text = "剪切";
   this.menu_EditCut.Click += new System.EventHandler(this.menu_EditCut_Click);
   //
   // menu_EditCopy
   //
   this.menu_EditCopy.Index = 1;
   this.menu_EditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
   this.menu_EditCopy.Text = "复制";
   this.menu_EditCopy.Click += new System.EventHandler(this.menu_EditCopy_Click);
   //
   // menuEditPaste
   //
   this.menuEditPaste.Index = 2;
   this.menuEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
   this.menuEditPaste.Text = "粘贴";
   this.menuEditPaste.Click += new System.EventHandler(this.menuEditPaste_Click);
   //
   // menuItem16
   //
   this.menuItem16.Index = 3;
   this.menuItem16.Text = "-";
   //
   // menu_EditSelAll
   //
   this.menu_EditSelAll.Index = 4;
   this.menu_EditSelAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
   this.menu_EditSelAll.Text = "全选";
   this.menu_EditSelAll.Click += new System.EventHandler(this.menu_EditSelAll_Click);
   //
   // menu_EditFind
   //
   this.menu_EditFind.Index = 5;
   this.menu_EditFind.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
   this.menu_EditFind.Text = "查找";
   this.menu_EditFind.Click += new System.EventHandler(this.menu_EditFind_Click);
   //
   // menu_View
   //
   this.menu_View.Index = 2;
   this.menu_View.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_ViewSource});
   this.menu_View.Text = "查看(&V)";
   //
   // menu_ViewSource
   //
   this.menu_ViewSource.Index = 0;
   this.menu_ViewSource.Text = "源文件";
   this.menu_ViewSource.Click += new System.EventHandler(this.menu_ViewSource_Click);
   //
   // menu_Tool
   //
   this.menu_Tool.Index = 3;
   this.menu_Tool.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_ToolInternet});
   this.menu_Tool.Text = "工具(&T)";
   //
   // menu_ToolInternet
   //
   this.menu_ToolInternet.Index = 0;
   this.menu_ToolInternet.Text = "Internet选项";
   this.menu_ToolInternet.Click += new System.EventHandler(this.menu_ToolInternet_Click);
   //
   // menu_Help
   //
   this.menu_Help.Index = 4;
   this.menu_Help.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menu_HelpAbout});
   this.menu_Help.Text = "帮助(&H)";
   //
   // menu_HelpAbout
   //
   this.menu_HelpAbout.Index = 0;
   this.menu_HelpAbout.Text = "关于";
   this.menu_HelpAbout.Click += new System.EventHandler(this.menu_HelpAbout_Click);
   //
   // imageList1
   //
   this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
   this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
   this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
   //
   // mainStatus
   //
   this.mainStatus.Location = new System.Drawing.Point(0, 297);
   this.mainStatus.Name = "mainStatus";
   this.mainStatus.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                        this.statusBarInfo});
   this.mainStatus.ShowPanels = true;
   this.mainStatus.Size = new System.Drawing.Size(616, 20);
   this.mainStatus.TabIndex = 6;
   //
   // statusBarInfo
   //
   this.statusBarInfo.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
   this.statusBarInfo.Width = 600;
   //
   // myErrorProvider
   //
   this.myErrorProvider.ContainerControl = this;
   //
   // mainToolBar
   //
   this.mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
   this.mainToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                         this.tbb_New,
                         this.toolBarButton1,
                         this.tbb_Back,
                         this.tbb_Forward,
                         this.toolBarButton2,
                         this.tbb_Stop,
                         this.tbb_Refresh,
                         this.tbb_GoHome,
                         this.toolBarButton3});
   this.mainToolBar.ButtonSize = new System.Drawing.Size(60, 22);
   this.mainToolBar.DropDownArrows = true;
   this.mainToolBar.ImageList = this.imageList1;
   this.mainToolBar.Location = new System.Drawing.Point(0, 0);
   this.mainToolBar.Name = "mainToolBar";
   this.mainToolBar.ShowToolTips = true;
   this.mainToolBar.Size = new System.Drawing.Size(616, 28);
   this.mainToolBar.TabIndex = 7;
   this.mainToolBar.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
   this.mainToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.mainToolBar_ButtonClick);
   //
   // tbb_Back
   //
   this.tbb_Back.ImageIndex = 1;
   this.tbb_Back.Text = "后退";
   this.tbb_Back.ToolTipText = "后退";
   //
   // tbb_Forward
   //
   this.tbb_Forward.ImageIndex = 2;
   this.tbb_Forward.Text = "前进";
   this.tbb_Forward.ToolTipText = "前进";
   //
   // toolBarButton2
   //
   this.toolBarButton2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
   //
   // tbb_Stop
   //
   this.tbb_Stop.ImageIndex = 3;
   this.tbb_Stop.Text = "停止";
   this.tbb_Stop.ToolTipText = "停止";
   //
   // tbb_Refresh
   //
   this.tbb_Refresh.ImageIndex = 4;
   this.tbb_Refresh.Text = "刷新";
   this.tbb_Refresh.ToolTipText = "刷新";
   //
   // tbb_GoHome
   //
   this.tbb_GoHome.ImageIndex = 5;
   this.tbb_GoHome.Text = "主页";
   this.tbb_GoHome.ToolTipText = "主页";
   //
   // toolBarButton3
   //
   this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
   //
   // mainTabCtrl
   //
   this.mainTabCtrl.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
   this.mainTabCtrl.Location = new System.Drawing.Point(8, 64);
   this.mainTabCtrl.Name = "mainTabCtrl";
   this.mainTabCtrl.SelectedIndex = 0;
   this.mainTabCtrl.Size = new System.Drawing.Size(560, 232);
   this.mainTabCtrl.TabIndex = 8;
   this.mainTabCtrl.DoubleClick += new System.EventHandler(this.mainTabCtrl_DoubleClick);
   this.mainTabCtrl.SelectedIndexChanged += new System.EventHandler(this.mainTabCtrl_SelectedIndexChanged);
   //
   // btnCloseCurrent
   //
   this.btnCloseCurrent.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.btnCloseCurrent.Location = new System.Drawing.Point(400, 40);
   this.btnCloseCurrent.Name = "btnCloseCurrent";
   this.btnCloseCurrent.Size = new System.Drawing.Size(88, 24);
   this.btnCloseCurrent.TabIndex = 9;
   this.btnCloseCurrent.Text = "关闭当前页";
   this.btnCloseCurrent.Click += new System.EventHandler(this.btnCloseCurrent_Click);
   //
   // btnCloseAll
   //
   this.btnCloseAll.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.btnCloseAll.Location = new System.Drawing.Point(496, 40);
   this.btnCloseAll.Name = "btnCloseAll";
   this.btnCloseAll.Size = new System.Drawing.Size(88, 24);
   this.btnCloseAll.TabIndex = 10;
   this.btnCloseAll.Text = "关闭所有页";
   this.btnCloseAll.Click += new System.EventHandler(this.btnCloseAll_Click);
   //
   // comboAddress
   //
   this.comboAddress.Location = new System.Drawing.Point(48, 40);
   this.comboAddress.Name = "comboAddress";
   this.comboAddress.Size = new System.Drawing.Size(264, 20);
   this.comboAddress.TabIndex = 11;
   this.comboAddress.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboAddress_KeyPress);
   //
   // menu_FileNew
   //
   this.menu_FileNew.Index = 0;
   this.menu_FileNew.Text = "新建";
   this.menu_FileNew.Click += new System.EventHandler(this.menu_FileNew_Click);
   //
   // tbb_New
   //
   this.tbb_New.ImageIndex = 0;
   this.tbb_New.Text = "新建";
   this.tbb_New.ToolTipText = "新建";
   //
   // toolBarButton1
   //
   this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
   //
   // MainForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(616, 317);
   this.Controls.Add(this.comboAddress);
   this.Controls.Add(this.btnCloseAll);
   this.Controls.Add(this.btnCloseCurrent);
   this.Controls.Add(this.mainTabCtrl);
   this.Controls.Add(this.mainToolBar);
   this.Controls.Add(this.mainStatus);
   this.Controls.Add(this.btnGo);
   this.Controls.Add(this.lblAddress);
   this.Controls.Add(this.mainBrowser);
   this.Menu = this.mainMenu1;
   this.Name = "MainForm";
   this.Text = "网页浏览器";
   this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
   this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged);
   this.Load += new System.EventHandler(this.MainForm_Load);
   this.DoubleClick += new System.EventHandler(this.MainForm_DoubleClick);
   ((System.ComponentModel.ISupportInitialize)(this.mainBrowser)).EndInit();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarInfo)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new MainForm());
  }

  private void comboAddress_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
   if(e.KeyChar == 13) //浏览指定网页
   {
    this.NavigateUrl(this.comboAddress.Text);
   }
   else
   {
    if (comboAddress.Text != "")
    {
     string strText = comboAddress.Text;
     for (int i=comboAddress.Items.Count-1;i>=0;i--) {
      this.comboAddress.Items.RemoveAt(i);
     }
     foreach (object obj in VisitedList) {
      string strList = obj as string;
      if (strList != null) {
       if (strList.StartsWith(strText)
        || strList.StartsWith("http://" + strText)
        || strList.StartsWith("http://www." + strText)) {
        this.comboAddress.Items.Add(strList);
       }
      }
     
     }
     this.comboAddress.DroppedDown = true;
    }
   }
  }
  private void MainForm_SizeChanged(object sender, System.EventArgs e)
  {
   ResizeWindow();
  }

  private void MainForm_Load(object sender, System.EventArgs e)
  {
   ResizeWindow();
   CreateNewWebBrowser();
   LoadFormerList();
  }
  private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   SaveFormerList();
  }
  #region Personal Methods
  private void NavigateUrl(string strUrl)
  {
   GetCurrentBrowser().Navigate(strUrl);
  }
  private void LoadFormerList()
  {
    FileInfo finfo = new FileInfo(FormerListFile);
    FileStream fs = finfo.Open(FileMode.OpenOrCreate,
     FileAccess.ReadWrite);
    StreamReader sr = new StreamReader(fs);
    string strList = sr.ReadLine();
    while(strList != null){
     VisitedList.Add(strList.Trim());
     strList = sr.ReadLine();
    }
    sr.Close();
    fs.Close();
  }
  private void SaveFormerList()
  {
   FileInfo finfo = new FileInfo(FormerListFile);
   FileStream fs = finfo.Open(
    FileMode.OpenOrCreate | FileMode.Truncate,
    FileAccess.ReadWrite);
   StreamWriter sw = new StreamWriter(fs);
   foreach (object obj in VisitedList) {
    string strList = obj as string;
    if(strList != null){
     sw.WriteLine(strList);
    }
   }
   sw.Close();
   fs.Close();
  }
  private AxSHDocVw.AxWebBrowser CreateNewWebBrowser()
  {
   AxSHDocVw.AxWebBrowser _axWebBrowser = new AxSHDocVw.AxWebBrowser();
   _axWebBrowser.Tag = new HE_WebBrowserTag();
   TabPage _TabPage = new TabPage();
   _TabPage.Controls.Add(_axWebBrowser);
   _axWebBrowser.Dock = DockStyle.Fill;
   _axWebBrowser.BeforeNavigate2 += new
    AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(
    this.mainBrowser_BeforeNavigate2);
   _axWebBrowser.DocumentComplete += new
    AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
    this.mainBrowser_DocumentComplete);
   _axWebBrowser.NavigateComplete2 += new
    AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(
    this.mainBrowser_NavigateComplete2);
   _axWebBrowser.NavigateError += new
    AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(
    this.mainBrowser_NavigateError);
   _axWebBrowser.NewWindow2 += new
    AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(
    this.mainBrowser_NewWindow2);
   _axWebBrowser.ProgressChange += new
    AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(
    this.mainBrowser_ProgressChange);
   _axWebBrowser.StatusTextChange += new
    AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(
    this.mainBrowser_StatusTextChange);
   _axWebBrowser.TitleChange += new
    AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(
    this.mainBrowser_TitleChange);
   _axWebBrowser.CommandStateChange += new
    AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(
    this.mainBrowser_CommandStateChange);

   this.mainTabCtrl.TabPages.Add(_TabPage);
   this.mainTabCtrl.SelectedTab = _TabPage;
   return _axWebBrowser;
  }
  private void ResizeWindow()
  {
   this.lblAddress.Left = 0;
   this.lblAddress.Width = 30;
   this.lblAddress.Top = this.mainToolBar.Bottom + 2;;
   this.lblAddress.Height = 25;

   this.comboAddress.Left = this.lblAddress.Right;
   this.comboAddress.Width = this.ClientRectangle.Width - this.lblAddress.Width - 220;
   this.comboAddress.Top = this.lblAddress.Top;
   this.comboAddress.Height = 25;
  
   this.btnGo.Left = this.comboAddress.Right;
   this.btnGo.Width = 60;
   this.btnCloseCurrent.Left = this.btnGo.Right;
   this.btnCloseCurrent.Width = 80;
   this.btnCloseAll.Width = 80;
   this.btnCloseAll.Left = this.btnCloseCurrent.Right;
   this.btnCloseCurrent.Top = this.btnCloseAll.Top = this.btnGo.Top = this.lblAddress.Top;
   this.btnCloseCurrent.Height = this.btnCloseAll.Height = this.btnGo.Height = 25;

   this.mainTabCtrl.Left = 0;
   this.mainTabCtrl.Top = this.lblAddress.Bottom;
   this.mainTabCtrl.Width = this.ClientRectangle.Width;
   this.mainTabCtrl.Height = this.mainStatus.Top - this.lblAddress.Bottom;
  }
  private enum MiscCommandTarget { Find = 1, ViewSource, Options }
  private AxSHDocVw.AxWebBrowser GetCurrentBrowser()
  {
   try
   {
    if(this.mainTabCtrl.SelectedTab.Controls.Count > 0)
    {
     AxSHDocVw.AxWebBrowser brow =
      (AxSHDocVw.AxWebBrowser)
      this.mainTabCtrl.SelectedTab.Controls[0];
     return brow;
    }
    else
    {
     return null;
    }
   }
   catch
   {
    return null;
   }
  }
  private mshtml.HTMLDocument GetDocument()
  {
   try
   {
    AxSHDocVw.AxWebBrowser brow = GetCurrentBrowser();
    if (brow != null) {
     mshtml.HTMLDocument htm =
      (mshtml.HTMLDocument)brow.Document;
     return htm;
    }
    else
    {
     return null;
    }
   }catch
   {
    return null;
   }
  } 
  public void ViewSource()
  {
   IOleCommandTarget cmdt;
   Object o = new object();
   try
   {
    cmdt = (IOleCommandTarget)GetDocument();
    cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,
     (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
   }catch{}
  }

  public void Find()
  {
   IOleCommandTarget cmdt;
   Object o = new object();
   try
   {
    cmdt = (IOleCommandTarget)GetDocument();
    cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,
     (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
   }catch{}
  }

  public void InternetOptions()
  {
   IOleCommandTarget cmdt;
   Object o = new object();
   try
   {
    cmdt = (IOleCommandTarget)GetDocument();
    cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,
     (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
   }catch{}
  }
  #endregion
  #region MainBrowser事件响应
  private void mainBrowser_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
  {
   this.statusBarInfo.Text = e.text;
  }

  private void mainBrowser_NewWindow3(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e)
  {
  }

  private void mainBrowser_WindowClosing(object sender, AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent e)
  {
   this.Close();
  }

  private void mainBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
  {
   Cursor.Current = Cursors.Default;
   mshtml.HTMLDocument doc = GetDocument();
   this.comboAddress.Text = doc.url;
   this.mainTabCtrl.SelectedTab.Text = doc.title;
   this.mainTabCtrl.SelectedTab.ToolTipText = doc.title;
   if (!VisitedList.Contains(doc.url)) {
    VisitedList.Add(doc.url);
   }
  }

  private void mainBrowser_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
  {
   Cursor.Current = Cursors.WaitCursor;
  }

  private void mainBrowser_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
  {
   AxSHDocVw.AxWebBrowser _axWebBrowser = CreateNewWebBrowser();
   e.ppDisp = _axWebBrowser.Application;
   _axWebBrowser.RegisterAsBrowser = true;  
  }

  private void mainBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
  {
  }

  private void mainBrowser_NavigateError(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateErrorEvent e)
  {
  
  }

  private void mainBrowser_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e)
  {
  
  }

  private void mainBrowser_TitleChange(object sender, AxSHDocVw.DWebBrowserEvents2_TitleChangeEvent e)
  {
  
  }

  private void mainBrowser_CommandStateChange(object sender, AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
  {
  
  }
  #endregion
  #region 按钮事件响应
  private void btnGo_Click(object sender, System.EventArgs e)
  {
   this.NavigateUrl(this.comboAddress.Text);
  } 
  private void btnCloseCurrent_Click(object sender, System.EventArgs e)
  {
   this.mainTabCtrl.SelectedTab.Dispose();
   if (this.mainTabCtrl.TabPages.Count == 0) {
    this.CreateNewWebBrowser();
   }
  }
  private void btnCloseAll_Click(object sender, System.EventArgs e)
  {
   for (int pagecount = this.mainTabCtrl.TabPages.Count;
    pagecount > 0;pagecount--) {
    this.mainTabCtrl.TabPages[pagecount-1].Dispose();
   }
   this.CreateNewWebBrowser();
  }
  #endregion
  #region Toolbar事件响应
  private void mainToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  {
   if(e.Button.Equals(this.tbb_New))
   {
    try
    {
     CreateNewWebBrowser();
    }
    catch{};
   }
   if(e.Button.Equals(this.tbb_Back))
   {
    try
    {
     GetCurrentBrowser().GoBack();
    }
    catch{};
   }
   if(e.Button.Equals(this.tbb_Forward))
   {
    try
    {
     GetCurrentBrowser().GoForward();
    }
    catch{};
   }
   if(e.Button.Equals(this.tbb_Stop))
   {
    GetCurrentBrowser().Stop();
   }
   if(e.Button.Equals(this.tbb_Refresh))
   {
    GetCurrentBrowser().Refresh();
   }
   if(e.Button.Equals(this.tbb_GoHome))
   {
    GetCurrentBrowser().GoHome();
   }
  }
  #endregion
  #region 菜单响应
  private void menu_FileNew_Click(object sender, System.EventArgs e)
  {
   CreateNewWebBrowser();
  } 
  private void menu_ToolInternet_Click(object sender, System.EventArgs e)
  {
   this.InternetOptions();
  }

  private void menu_FileOpen_Click(object sender, System.EventArgs e)
  {
   if (GetCurrentBrowser().LocationURL != "")
   {
    GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_OPEN,
     SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
   }
  }

  private void menu_FileSave_Click(object sender, System.EventArgs e)
  {
   if (GetCurrentBrowser().LocationURL != "") {
    GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVEAS,
     SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
   }
  }

  private void menu_FileSaveAs_Click(object sender, System.EventArgs e)
  {
   if (GetCurrentBrowser().LocationURL != "")
   {
    GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVEAS,
     SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
   }
  }
  private void menu_FileProperty_Click(object sender, System.EventArgs e)
  {
   if(GetCurrentBrowser().LocationURL!="")
   {
    GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_PROPERTIES,
     SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
   }
  }
  private void menu_EditFind_Click(object sender, System.EventArgs e)
  {
   this.Find();
  }

  private void menu_EditSelAll_Click(object sender, System.EventArgs e)
  {
   GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SELECTALL,
    SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  }

  private void menu_EditCut_Click(object sender, System.EventArgs e)
  {
   GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_CUT,
    SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  }

  private void menu_EditCopy_Click(object sender, System.EventArgs e)
  {
   GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY,
    SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  }

  private void menuEditPaste_Click(object sender, System.EventArgs e)
  {
   GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_PASTE,
    SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  }

  private void menu_ViewSource_Click(object sender, System.EventArgs e)
  {
   this.ViewSource();
  }

  private void menu_AddToFavor_Click(object sender, System.EventArgs e)
  {
  }

  private void menu_HelpAbout_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show("网页浏览器1.0","网页浏览器",MessageBoxButtons.OK,
    MessageBoxIcon.Information);
  }
  private void menu_FileExit_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }
  #endregion
  #region TabControl 事件响应
  private void mainTabCtrl_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   if (GetDocument() != null) {
    this.comboAddress.Text = GetDocument().url;
   }
   else
   {
    this.comboAddress.Text = "about:blank";
   }
  }
  private bool IsSolved = false;
  private void mainTabCtrl_DoubleClick(object sender, System.EventArgs e)
  {
   try
   {
    for (int i=this.mainTabCtrl.TabCount-1;i>=0;i--)
    {
     Point pos = this.mainTabCtrl.PointToClient(Cursor.Position);
     if (this.mainTabCtrl.GetTabRect(i).Contains(pos))
     {
      this.mainTabCtrl.TabPages[i].Dispose();
      if(this.mainTabCtrl.TabCount == 0)
       CreateNewWebBrowser();
      IsSolved = true;
      return;
     }
    }
   }
   catch{} 
  }
  private void MainForm_DoubleClick(object sender, System.EventArgs e)
  {
   if (this.mainTabCtrl.ClientRectangle.Contains(
    this.PointToClient(Cursor.Position))) {
    IsSolved = false;
    this.mainTabCtrl_DoubleClick(sender,e);
    if(!IsSolved)
    {
     CreateNewWebBrowser();
    }
   }
  }
  #endregion
 }
}

上一页  [1] [2] [3] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有