MainForm.cs
上传用户:unitenet
上传日期:2022-02-13
资源大小:200k
文件大小:34k
源码类别:

浏览器

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. namespace WebBrowser
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class MainForm : System.Windows.Forms.Form
  14. {
  15. #region Controls
  16. private System.Windows.Forms.Label lblAddress;
  17. private AxSHDocVw.AxWebBrowser mainBrowser;
  18. private System.Windows.Forms.Button btnGo;
  19. private System.Windows.Forms.MainMenu mainMenu1;
  20. private System.Windows.Forms.MenuItem menuItem5;
  21. private System.Windows.Forms.ImageList imageList1;
  22. private System.Windows.Forms.StatusBar mainStatus;
  23. private System.Windows.Forms.StatusBarPanel statusBarInfo;
  24. private System.Windows.Forms.ErrorProvider myErrorProvider;
  25. private System.Windows.Forms.MenuItem menu_File;
  26. private System.Windows.Forms.MenuItem menu_FileOpen;
  27. private System.Windows.Forms.MenuItem menu_FileSave;
  28. private System.Windows.Forms.MenuItem menu_FileSaveAs;
  29. private System.Windows.Forms.MenuItem menu_FileExit;
  30. private System.Windows.Forms.MenuItem menuItem16;
  31. private System.Windows.Forms.MenuItem menu_FileProperty;
  32. private System.Windows.Forms.MenuItem menu_EditCut;
  33. private System.Windows.Forms.MenuItem menu_EditCopy;
  34. private System.Windows.Forms.MenuItem menuEditPaste;
  35. private System.Windows.Forms.MenuItem menu_EditSelAll;
  36. private System.Windows.Forms.MenuItem menu_EditFind;
  37. private System.Windows.Forms.MenuItem menu_Edit;
  38. private System.Windows.Forms.MenuItem menu_View;
  39. private System.Windows.Forms.MenuItem menu_Tool;
  40. private System.Windows.Forms.MenuItem menu_Help;
  41. private System.Windows.Forms.MenuItem menu_HelpAbout;
  42. private System.Windows.Forms.MenuItem menu_ViewSource;
  43. private System.Windows.Forms.MenuItem menu_ToolInternet;
  44. private System.Windows.Forms.ToolBar mainToolBar;
  45. private System.Windows.Forms.TabControl mainTabCtrl;
  46. private System.Windows.Forms.Button btnCloseCurrent;
  47. private System.Windows.Forms.Button btnCloseAll;
  48. private System.Windows.Forms.ToolBarButton tbb_Back;
  49. private System.Windows.Forms.ToolBarButton tbb_Forward;
  50. private System.Windows.Forms.ToolBarButton tbb_Stop;
  51. private System.Windows.Forms.ToolBarButton tbb_Refresh;
  52. private System.Windows.Forms.ToolBarButton tbb_GoHome;
  53. private System.ComponentModel.IContainer components;
  54. private System.Windows.Forms.ComboBox comboAddress;
  55. private System.Windows.Forms.MenuItem menu_FileNew;
  56. private System.Windows.Forms.ToolBarButton tbb_New;
  57. private System.Windows.Forms.ToolBarButton toolBarButton2;
  58. private System.Windows.Forms.ToolBarButton toolBarButton3;
  59. private System.Windows.Forms.ToolBarButton toolBarButton1;
  60. #endregion
  61. private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");
  62. private string FormerListFile = "FormerList.dat";
  63. private ArrayList VisitedList = new ArrayList();
  64. public MainForm()
  65. {
  66. //
  67. // Windows 窗体设计器支持所必需的
  68. //
  69. InitializeComponent();
  70. //
  71. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  72. //
  73. }
  74. /// <summary>
  75. /// 清理所有正在使用的资源。
  76. /// </summary>
  77. protected override void Dispose( bool disposing )
  78. {
  79. if( disposing )
  80. {
  81. if (components != null) 
  82. {
  83. components.Dispose();
  84. }
  85. }
  86. base.Dispose( disposing );
  87. }
  88. #region Windows 窗体设计器生成的代码
  89. /// <summary>
  90. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  91. /// 此方法的内容。
  92. /// </summary>
  93. private void InitializeComponent()
  94. {
  95. this.components = new System.ComponentModel.Container();
  96. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
  97. this.mainBrowser = new AxSHDocVw.AxWebBrowser();
  98. this.lblAddress = new System.Windows.Forms.Label();
  99. this.btnGo = new System.Windows.Forms.Button();
  100. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  101. this.menu_File = new System.Windows.Forms.MenuItem();
  102. this.menu_FileNew = new System.Windows.Forms.MenuItem();
  103. this.menu_FileOpen = new System.Windows.Forms.MenuItem();
  104. this.menu_FileSave = new System.Windows.Forms.MenuItem();
  105. this.menu_FileSaveAs = new System.Windows.Forms.MenuItem();
  106. this.menuItem5 = new System.Windows.Forms.MenuItem();
  107. this.menu_FileProperty = new System.Windows.Forms.MenuItem();
  108. this.menu_FileExit = new System.Windows.Forms.MenuItem();
  109. this.menu_Edit = new System.Windows.Forms.MenuItem();
  110. this.menu_EditCut = new System.Windows.Forms.MenuItem();
  111. this.menu_EditCopy = new System.Windows.Forms.MenuItem();
  112. this.menuEditPaste = new System.Windows.Forms.MenuItem();
  113. this.menuItem16 = new System.Windows.Forms.MenuItem();
  114. this.menu_EditSelAll = new System.Windows.Forms.MenuItem();
  115. this.menu_EditFind = new System.Windows.Forms.MenuItem();
  116. this.menu_View = new System.Windows.Forms.MenuItem();
  117. this.menu_ViewSource = new System.Windows.Forms.MenuItem();
  118. this.menu_Tool = new System.Windows.Forms.MenuItem();
  119. this.menu_ToolInternet = new System.Windows.Forms.MenuItem();
  120. this.menu_Help = new System.Windows.Forms.MenuItem();
  121. this.menu_HelpAbout = new System.Windows.Forms.MenuItem();
  122. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  123. this.mainStatus = new System.Windows.Forms.StatusBar();
  124. this.statusBarInfo = new System.Windows.Forms.StatusBarPanel();
  125. this.myErrorProvider = new System.Windows.Forms.ErrorProvider();
  126. this.mainToolBar = new System.Windows.Forms.ToolBar();
  127. this.tbb_New = new System.Windows.Forms.ToolBarButton();
  128. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  129. this.tbb_Back = new System.Windows.Forms.ToolBarButton();
  130. this.tbb_Forward = new System.Windows.Forms.ToolBarButton();
  131. this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
  132. this.tbb_Stop = new System.Windows.Forms.ToolBarButton();
  133. this.tbb_Refresh = new System.Windows.Forms.ToolBarButton();
  134. this.tbb_GoHome = new System.Windows.Forms.ToolBarButton();
  135. this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
  136. this.mainTabCtrl = new System.Windows.Forms.TabControl();
  137. this.btnCloseCurrent = new System.Windows.Forms.Button();
  138. this.btnCloseAll = new System.Windows.Forms.Button();
  139. this.comboAddress = new System.Windows.Forms.ComboBox();
  140. ((System.ComponentModel.ISupportInitialize)(this.mainBrowser)).BeginInit();
  141. ((System.ComponentModel.ISupportInitialize)(this.statusBarInfo)).BeginInit();
  142. this.SuspendLayout();
  143. // 
  144. // mainBrowser
  145. // 
  146. this.mainBrowser.Enabled = true;
  147. this.mainBrowser.Location = new System.Drawing.Point(576, 104);
  148. this.mainBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mainBrowser.OcxState")));
  149. this.mainBrowser.Size = new System.Drawing.Size(696, 248);
  150. this.mainBrowser.TabIndex = 0;
  151. this.mainBrowser.Visible = false;
  152. this.mainBrowser.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.mainBrowser_StatusTextChange);
  153. this.mainBrowser.WindowClosing += new AxSHDocVw.DWebBrowserEvents2_WindowClosingEventHandler(this.mainBrowser_WindowClosing);
  154. this.mainBrowser.NewWindow3 += new AxSHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(this.mainBrowser_NewWindow3);
  155. this.mainBrowser.NavigateError += new AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(this.mainBrowser_NavigateError);
  156. this.mainBrowser.CommandStateChange += new AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(this.mainBrowser_CommandStateChange);
  157. this.mainBrowser.TitleChange += new AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(this.mainBrowser_TitleChange);
  158. this.mainBrowser.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.mainBrowser_NavigateComplete2);
  159. this.mainBrowser.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.mainBrowser_DocumentComplete);
  160. this.mainBrowser.ProgressChange += new AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(this.mainBrowser_ProgressChange);
  161. this.mainBrowser.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.mainBrowser_NewWindow2);
  162. this.mainBrowser.BeforeNavigate2 += new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.mainBrowser_BeforeNavigate2);
  163. // 
  164. // lblAddress
  165. // 
  166. this.lblAddress.Location = new System.Drawing.Point(0, 48);
  167. this.lblAddress.Name = "lblAddress";
  168. this.lblAddress.Size = new System.Drawing.Size(48, 16);
  169. this.lblAddress.TabIndex = 1;
  170. this.lblAddress.Text = "地址";
  171. // 
  172. // btnGo
  173. // 
  174. this.btnGo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  175. this.btnGo.Location = new System.Drawing.Point(320, 40);
  176. this.btnGo.Name = "btnGo";
  177. this.btnGo.Size = new System.Drawing.Size(75, 24);
  178. this.btnGo.TabIndex = 3;
  179. this.btnGo.Text = "转到";
  180. this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
  181. // 
  182. // mainMenu1
  183. // 
  184. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  185.   this.menu_File,
  186.   this.menu_Edit,
  187.   this.menu_View,
  188.   this.menu_Tool,
  189.   this.menu_Help});
  190. // 
  191. // menu_File
  192. // 
  193. this.menu_File.Index = 0;
  194. this.menu_File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  195.   this.menu_FileNew,
  196.   this.menu_FileOpen,
  197.   this.menu_FileSave,
  198.   this.menu_FileSaveAs,
  199.   this.menuItem5,
  200.   this.menu_FileProperty,
  201.   this.menu_FileExit});
  202. this.menu_File.Text = "文件(&F)";
  203. // 
  204. // menu_FileNew
  205. // 
  206. this.menu_FileNew.Index = 0;
  207. this.menu_FileNew.Text = "新建";
  208. this.menu_FileNew.Click += new System.EventHandler(this.menu_FileNew_Click);
  209. // 
  210. // menu_FileOpen
  211. // 
  212. this.menu_FileOpen.Index = 1;
  213. this.menu_FileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
  214. this.menu_FileOpen.Text = "打开";
  215. this.menu_FileOpen.Click += new System.EventHandler(this.menu_FileOpen_Click);
  216. // 
  217. // menu_FileSave
  218. // 
  219. this.menu_FileSave.Index = 2;
  220. this.menu_FileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
  221. this.menu_FileSave.Text = "保存";
  222. this.menu_FileSave.Click += new System.EventHandler(this.menu_FileSave_Click);
  223. // 
  224. // menu_FileSaveAs
  225. // 
  226. this.menu_FileSaveAs.Index = 3;
  227. this.menu_FileSaveAs.Text = "另存为...";
  228. this.menu_FileSaveAs.Click += new System.EventHandler(this.menu_FileSaveAs_Click);
  229. // 
  230. // menuItem5
  231. // 
  232. this.menuItem5.Index = 4;
  233. this.menuItem5.Text = "-";
  234. // 
  235. // menu_FileProperty
  236. // 
  237. this.menu_FileProperty.Index = 5;
  238. this.menu_FileProperty.Text = "属性(&R)";
  239. this.menu_FileProperty.Click += new System.EventHandler(this.menu_FileProperty_Click);
  240. // 
  241. // menu_FileExit
  242. // 
  243. this.menu_FileExit.Index = 6;
  244. this.menu_FileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
  245. this.menu_FileExit.Text = "退出(&C)";
  246. this.menu_FileExit.Click += new System.EventHandler(this.menu_FileExit_Click);
  247. // 
  248. // menu_Edit
  249. // 
  250. this.menu_Edit.Index = 1;
  251. this.menu_Edit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  252.   this.menu_EditCut,
  253.   this.menu_EditCopy,
  254.   this.menuEditPaste,
  255.   this.menuItem16,
  256.   this.menu_EditSelAll,
  257.   this.menu_EditFind});
  258. this.menu_Edit.Text = "编辑(&E)";
  259. // 
  260. // menu_EditCut
  261. // 
  262. this.menu_EditCut.Index = 0;
  263. this.menu_EditCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
  264. this.menu_EditCut.Text = "剪切";
  265. this.menu_EditCut.Click += new System.EventHandler(this.menu_EditCut_Click);
  266. // 
  267. // menu_EditCopy
  268. // 
  269. this.menu_EditCopy.Index = 1;
  270. this.menu_EditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
  271. this.menu_EditCopy.Text = "复制";
  272. this.menu_EditCopy.Click += new System.EventHandler(this.menu_EditCopy_Click);
  273. // 
  274. // menuEditPaste
  275. // 
  276. this.menuEditPaste.Index = 2;
  277. this.menuEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
  278. this.menuEditPaste.Text = "粘贴";
  279. this.menuEditPaste.Click += new System.EventHandler(this.menuEditPaste_Click);
  280. // 
  281. // menuItem16
  282. // 
  283. this.menuItem16.Index = 3;
  284. this.menuItem16.Text = "-";
  285. // 
  286. // menu_EditSelAll
  287. // 
  288. this.menu_EditSelAll.Index = 4;
  289. this.menu_EditSelAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
  290. this.menu_EditSelAll.Text = "全选";
  291. this.menu_EditSelAll.Click += new System.EventHandler(this.menu_EditSelAll_Click);
  292. // 
  293. // menu_EditFind
  294. // 
  295. this.menu_EditFind.Index = 5;
  296. this.menu_EditFind.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
  297. this.menu_EditFind.Text = "查找";
  298. this.menu_EditFind.Click += new System.EventHandler(this.menu_EditFind_Click);
  299. // 
  300. // menu_View
  301. // 
  302. this.menu_View.Index = 2;
  303. this.menu_View.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  304.   this.menu_ViewSource});
  305. this.menu_View.Text = "查看(&V)";
  306. // 
  307. // menu_ViewSource
  308. // 
  309. this.menu_ViewSource.Index = 0;
  310. this.menu_ViewSource.Text = "源文件";
  311. this.menu_ViewSource.Click += new System.EventHandler(this.menu_ViewSource_Click);
  312. // 
  313. // menu_Tool
  314. // 
  315. this.menu_Tool.Index = 3;
  316. this.menu_Tool.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  317.   this.menu_ToolInternet});
  318. this.menu_Tool.Text = "工具(&T)";
  319. // 
  320. // menu_ToolInternet
  321. // 
  322. this.menu_ToolInternet.Index = 0;
  323. this.menu_ToolInternet.Text = "Internet选项";
  324. this.menu_ToolInternet.Click += new System.EventHandler(this.menu_ToolInternet_Click);
  325. // 
  326. // menu_Help
  327. // 
  328. this.menu_Help.Index = 4;
  329. this.menu_Help.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  330.   this.menu_HelpAbout});
  331. this.menu_Help.Text = "帮助(&H)";
  332. // 
  333. // menu_HelpAbout
  334. // 
  335. this.menu_HelpAbout.Index = 0;
  336. this.menu_HelpAbout.Text = "关于";
  337. this.menu_HelpAbout.Click += new System.EventHandler(this.menu_HelpAbout_Click);
  338. // 
  339. // imageList1
  340. // 
  341. this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
  342. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  343. this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
  344. // 
  345. // mainStatus
  346. // 
  347. this.mainStatus.Location = new System.Drawing.Point(0, 297);
  348. this.mainStatus.Name = "mainStatus";
  349. this.mainStatus.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
  350.   this.statusBarInfo});
  351. this.mainStatus.ShowPanels = true;
  352. this.mainStatus.Size = new System.Drawing.Size(616, 20);
  353. this.mainStatus.TabIndex = 6;
  354. // 
  355. // statusBarInfo
  356. // 
  357. this.statusBarInfo.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
  358. this.statusBarInfo.Width = 600;
  359. // 
  360. // myErrorProvider
  361. // 
  362. this.myErrorProvider.ContainerControl = this;
  363. // 
  364. // mainToolBar
  365. // 
  366. this.mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
  367. this.mainToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  368.    this.tbb_New,
  369.    this.toolBarButton1,
  370.    this.tbb_Back,
  371.    this.tbb_Forward,
  372.    this.toolBarButton2,
  373.    this.tbb_Stop,
  374.    this.tbb_Refresh,
  375.    this.tbb_GoHome,
  376.    this.toolBarButton3});
  377. this.mainToolBar.ButtonSize = new System.Drawing.Size(60, 22);
  378. this.mainToolBar.DropDownArrows = true;
  379. this.mainToolBar.ImageList = this.imageList1;
  380. this.mainToolBar.Location = new System.Drawing.Point(0, 0);
  381. this.mainToolBar.Name = "mainToolBar";
  382. this.mainToolBar.ShowToolTips = true;
  383. this.mainToolBar.Size = new System.Drawing.Size(616, 28);
  384. this.mainToolBar.TabIndex = 7;
  385. this.mainToolBar.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
  386. this.mainToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.mainToolBar_ButtonClick);
  387. // 
  388. // tbb_New
  389. // 
  390. this.tbb_New.ImageIndex = 0;
  391. this.tbb_New.Text = "新建";
  392. this.tbb_New.ToolTipText = "新建";
  393. // 
  394. // toolBarButton1
  395. // 
  396. this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  397. // 
  398. // tbb_Back
  399. // 
  400. this.tbb_Back.ImageIndex = 1;
  401. this.tbb_Back.Text = "后退";
  402. this.tbb_Back.ToolTipText = "后退";
  403. // 
  404. // tbb_Forward
  405. // 
  406. this.tbb_Forward.ImageIndex = 2;
  407. this.tbb_Forward.Text = "前进";
  408. this.tbb_Forward.ToolTipText = "前进";
  409. // 
  410. // toolBarButton2
  411. // 
  412. this.toolBarButton2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  413. // 
  414. // tbb_Stop
  415. // 
  416. this.tbb_Stop.ImageIndex = 3;
  417. this.tbb_Stop.Text = "停止";
  418. this.tbb_Stop.ToolTipText = "停止";
  419. // 
  420. // tbb_Refresh
  421. // 
  422. this.tbb_Refresh.ImageIndex = 4;
  423. this.tbb_Refresh.Text = "刷新";
  424. this.tbb_Refresh.ToolTipText = "刷新";
  425. // 
  426. // tbb_GoHome
  427. // 
  428. this.tbb_GoHome.ImageIndex = 5;
  429. this.tbb_GoHome.Text = "主页";
  430. this.tbb_GoHome.ToolTipText = "主页";
  431. // 
  432. // toolBarButton3
  433. // 
  434. this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  435. // 
  436. // mainTabCtrl
  437. // 
  438. this.mainTabCtrl.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
  439. this.mainTabCtrl.Location = new System.Drawing.Point(8, 64);
  440. this.mainTabCtrl.Name = "mainTabCtrl";
  441. this.mainTabCtrl.SelectedIndex = 0;
  442. this.mainTabCtrl.Size = new System.Drawing.Size(560, 232);
  443. this.mainTabCtrl.TabIndex = 8;
  444. this.mainTabCtrl.DoubleClick += new System.EventHandler(this.mainTabCtrl_DoubleClick);
  445. this.mainTabCtrl.SelectedIndexChanged += new System.EventHandler(this.mainTabCtrl_SelectedIndexChanged);
  446. // 
  447. // btnCloseCurrent
  448. // 
  449. this.btnCloseCurrent.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  450. this.btnCloseCurrent.Location = new System.Drawing.Point(400, 40);
  451. this.btnCloseCurrent.Name = "btnCloseCurrent";
  452. this.btnCloseCurrent.Size = new System.Drawing.Size(88, 24);
  453. this.btnCloseCurrent.TabIndex = 9;
  454. this.btnCloseCurrent.Text = "关闭当前页";
  455. this.btnCloseCurrent.Click += new System.EventHandler(this.btnCloseCurrent_Click);
  456. // 
  457. // btnCloseAll
  458. // 
  459. this.btnCloseAll.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  460. this.btnCloseAll.Location = new System.Drawing.Point(496, 40);
  461. this.btnCloseAll.Name = "btnCloseAll";
  462. this.btnCloseAll.Size = new System.Drawing.Size(88, 24);
  463. this.btnCloseAll.TabIndex = 10;
  464. this.btnCloseAll.Text = "关闭所有页";
  465. this.btnCloseAll.Click += new System.EventHandler(this.btnCloseAll_Click);
  466. // 
  467. // comboAddress
  468. // 
  469. this.comboAddress.Location = new System.Drawing.Point(48, 40);
  470. this.comboAddress.Name = "comboAddress";
  471. this.comboAddress.Size = new System.Drawing.Size(264, 20);
  472. this.comboAddress.TabIndex = 11;
  473. this.comboAddress.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboAddress_KeyPress);
  474. this.comboAddress.SelectedIndexChanged += new System.EventHandler(this.comboAddress_SelectedIndexChanged);
  475. // 
  476. // MainForm
  477. // 
  478. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  479. this.ClientSize = new System.Drawing.Size(616, 317);
  480. this.Controls.Add(this.comboAddress);
  481. this.Controls.Add(this.btnCloseAll);
  482. this.Controls.Add(this.btnCloseCurrent);
  483. this.Controls.Add(this.mainTabCtrl);
  484. this.Controls.Add(this.mainToolBar);
  485. this.Controls.Add(this.mainStatus);
  486. this.Controls.Add(this.btnGo);
  487. this.Controls.Add(this.lblAddress);
  488. this.Controls.Add(this.mainBrowser);
  489. this.Menu = this.mainMenu1;
  490. this.Name = "MainForm";
  491. this.Text = "网页浏览器";
  492. this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
  493. this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged);
  494. this.Load += new System.EventHandler(this.MainForm_Load);
  495. this.DoubleClick += new System.EventHandler(this.MainForm_DoubleClick);
  496. ((System.ComponentModel.ISupportInitialize)(this.mainBrowser)).EndInit();
  497. ((System.ComponentModel.ISupportInitialize)(this.statusBarInfo)).EndInit();
  498. this.ResumeLayout(false);
  499. }
  500. #endregion
  501. /// <summary>
  502. /// 应用程序的主入口点。
  503. /// </summary>
  504. [STAThread]
  505. static void Main() 
  506. {
  507. Application.Run(new MainForm());
  508. }
  509. private void comboAddress_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  510. {
  511. if(e.KeyChar == 13) //浏览指定网页
  512. {
  513. this.NavigateUrl(this.comboAddress.Text);
  514. }
  515. else
  516. {
  517. if (comboAddress.Text != "") 
  518. {
  519. string strText = comboAddress.Text;
  520. for (int i=comboAddress.Items.Count-1;i>=0;i--) {
  521. this.comboAddress.Items.RemoveAt(i);
  522. }
  523. foreach (object obj in VisitedList) {
  524. string strList = obj as string;
  525. if (strList != null) {
  526. if (strList.StartsWith(strText)
  527. || strList.StartsWith("http://" + strText)
  528. || strList.StartsWith("http://www." + strText)) {
  529. this.comboAddress.Items.Add(strList);
  530. }
  531. }
  532. }
  533. this.comboAddress.DroppedDown = true;
  534. }
  535. }
  536. }
  537. private void MainForm_SizeChanged(object sender, System.EventArgs e)
  538. {
  539. ResizeWindow();
  540. }
  541. private void MainForm_Load(object sender, System.EventArgs e)
  542. {
  543. ResizeWindow();
  544. CreateNewWebBrowser();
  545. LoadFormerList();
  546. }
  547. private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  548. {
  549. SaveFormerList();
  550. }
  551. #region Personal Methods
  552. private void NavigateUrl(string strUrl)
  553. {
  554. GetCurrentBrowser().Navigate(strUrl);
  555. }
  556. private void LoadFormerList()
  557. {
  558. FileInfo finfo = new FileInfo(FormerListFile);
  559. FileStream fs = finfo.Open(FileMode.OpenOrCreate,
  560. FileAccess.ReadWrite);
  561. StreamReader sr = new StreamReader(fs);
  562. string strList = sr.ReadLine();
  563. while(strList != null){
  564. VisitedList.Add(strList.Trim());
  565. strList = sr.ReadLine();
  566. }
  567. sr.Close();
  568. fs.Close();
  569. }
  570. private void SaveFormerList()
  571. {
  572. FileInfo finfo = new FileInfo(FormerListFile);
  573. FileStream fs = finfo.Open(
  574. FileMode.OpenOrCreate | FileMode.Truncate,
  575. FileAccess.ReadWrite);
  576. StreamWriter sw = new StreamWriter(fs);
  577. foreach (object obj in VisitedList) {
  578. string strList = obj as string;
  579. if(strList != null){
  580. sw.WriteLine(strList);
  581. }
  582. }
  583. sw.Close();
  584. fs.Close();
  585. }
  586. private AxSHDocVw.AxWebBrowser CreateNewWebBrowser()
  587. {
  588. AxSHDocVw.AxWebBrowser _axWebBrowser = new AxSHDocVw.AxWebBrowser();
  589. _axWebBrowser.Tag = new HE_WebBrowserTag();
  590. TabPage _TabPage = new TabPage();
  591. _TabPage.Controls.Add(_axWebBrowser);
  592. _axWebBrowser.Dock = DockStyle.Fill;
  593. _axWebBrowser.BeforeNavigate2 += new 
  594. AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(
  595. this.mainBrowser_BeforeNavigate2);
  596. _axWebBrowser.DocumentComplete += new 
  597. AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
  598. this.mainBrowser_DocumentComplete);
  599. _axWebBrowser.NavigateComplete2 += new 
  600. AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(
  601. this.mainBrowser_NavigateComplete2);
  602. _axWebBrowser.NavigateError += new 
  603. AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(
  604. this.mainBrowser_NavigateError);
  605. _axWebBrowser.NewWindow2 += new 
  606. AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(
  607. this.mainBrowser_NewWindow2);
  608. _axWebBrowser.ProgressChange += new 
  609. AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(
  610. this.mainBrowser_ProgressChange);
  611. _axWebBrowser.StatusTextChange += new 
  612. AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(
  613. this.mainBrowser_StatusTextChange);
  614. _axWebBrowser.TitleChange += new 
  615. AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(
  616. this.mainBrowser_TitleChange);
  617. _axWebBrowser.CommandStateChange += new 
  618. AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(
  619. this.mainBrowser_CommandStateChange);
  620. this.mainTabCtrl.TabPages.Add(_TabPage);
  621. this.mainTabCtrl.SelectedTab = _TabPage;
  622. return _axWebBrowser;
  623. }
  624. private void ResizeWindow()
  625. {
  626. this.lblAddress.Left = 0;
  627. this.lblAddress.Width = 30;
  628. this.lblAddress.Top = this.mainToolBar.Bottom + 2;;
  629. this.lblAddress.Height = 25;
  630. this.comboAddress.Left = this.lblAddress.Right;
  631. this.comboAddress.Width = this.ClientRectangle.Width - this.lblAddress.Width - 220;
  632. this.comboAddress.Top = this.lblAddress.Top;
  633. this.comboAddress.Height = 25;
  634. this.btnGo.Left = this.comboAddress.Right;
  635. this.btnGo.Width = 60;
  636. this.btnCloseCurrent.Left = this.btnGo.Right;
  637. this.btnCloseCurrent.Width = 80;
  638. this.btnCloseAll.Width = 80;
  639. this.btnCloseAll.Left = this.btnCloseCurrent.Right;
  640. this.btnCloseCurrent.Top = this.btnCloseAll.Top = this.btnGo.Top = this.lblAddress.Top;
  641. this.btnCloseCurrent.Height = this.btnCloseAll.Height = this.btnGo.Height = 25;
  642. this.mainTabCtrl.Left = 0;
  643. this.mainTabCtrl.Top = this.lblAddress.Bottom;
  644. this.mainTabCtrl.Width = this.ClientRectangle.Width;
  645. this.mainTabCtrl.Height = this.mainStatus.Top - this.lblAddress.Bottom;
  646. }
  647. private enum MiscCommandTarget { Find = 1, ViewSource, Options }
  648. private AxSHDocVw.AxWebBrowser GetCurrentBrowser()
  649. {
  650. try
  651. {
  652. if(this.mainTabCtrl.SelectedTab.Controls.Count > 0)
  653. {
  654. AxSHDocVw.AxWebBrowser brow =
  655. (AxSHDocVw.AxWebBrowser)
  656. this.mainTabCtrl.SelectedTab.Controls[0];
  657. return brow;
  658. }
  659. else
  660. {
  661. return null;
  662. }
  663. }
  664. catch
  665. {
  666. return null;
  667. }
  668. }
  669. private mshtml.HTMLDocument GetDocument()
  670. {
  671. try
  672. {
  673. AxSHDocVw.AxWebBrowser brow = GetCurrentBrowser();
  674. if (brow != null) {
  675. mshtml.HTMLDocument htm =
  676. (mshtml.HTMLDocument)brow.Document;
  677. return htm;
  678. }
  679. else
  680. {
  681. return null;
  682. }
  683. }catch
  684. {
  685. return null;
  686. }
  687. }
  688. public void ViewSource()
  689. {
  690. IOleCommandTarget cmdt;
  691. Object o = new object();
  692. try
  693. {
  694. cmdt = (IOleCommandTarget)GetDocument();
  695. cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,
  696. (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
  697. }catch{}
  698. }
  699. public void Find()
  700. {
  701. IOleCommandTarget cmdt;
  702. Object o = new object();
  703. try
  704. {
  705. cmdt = (IOleCommandTarget)GetDocument();
  706. cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,
  707. (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
  708. }catch{}
  709. }
  710. public void InternetOptions()
  711. {
  712. IOleCommandTarget cmdt;
  713. Object o = new object();
  714. try
  715. {
  716. cmdt = (IOleCommandTarget)GetDocument();
  717. cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,
  718. (uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
  719. }catch{}
  720. }
  721. #endregion
  722. #region MainBrowser事件响应
  723. private void mainBrowser_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
  724. {
  725. this.statusBarInfo.Text = e.text;
  726. }
  727. private void mainBrowser_NewWindow3(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e)
  728. {
  729. }
  730. private void mainBrowser_WindowClosing(object sender, AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent e)
  731. {
  732. this.Close();
  733. }
  734. private void mainBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
  735. {
  736. Cursor.Current = Cursors.Default;
  737. mshtml.HTMLDocument doc = GetDocument();
  738. this.comboAddress.Text = doc.url;
  739. this.mainTabCtrl.SelectedTab.Text = doc.title;
  740. this.mainTabCtrl.SelectedTab.ToolTipText = doc.title;
  741. if (!VisitedList.Contains(doc.url)) {
  742. VisitedList.Add(doc.url);
  743. }
  744. }
  745. private void mainBrowser_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
  746. {
  747. Cursor.Current = Cursors.WaitCursor;
  748. }
  749. private void mainBrowser_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
  750. {
  751. AxSHDocVw.AxWebBrowser _axWebBrowser = CreateNewWebBrowser();
  752. e.ppDisp = _axWebBrowser.Application;
  753. _axWebBrowser.RegisterAsBrowser = true;
  754. }
  755. private void mainBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
  756. {
  757. }
  758. private void mainBrowser_NavigateError(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateErrorEvent e)
  759. {
  760. }
  761. private void mainBrowser_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e)
  762. {
  763. }
  764. private void mainBrowser_TitleChange(object sender, AxSHDocVw.DWebBrowserEvents2_TitleChangeEvent e)
  765. {
  766. }
  767. private void mainBrowser_CommandStateChange(object sender, AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
  768. {
  769. }
  770. #endregion
  771. #region 按钮事件响应
  772. private void btnGo_Click(object sender, System.EventArgs e)
  773. {
  774. this.NavigateUrl(this.comboAddress.Text);
  775. }
  776. private void btnCloseCurrent_Click(object sender, System.EventArgs e)
  777. {
  778. this.mainTabCtrl.SelectedTab.Dispose();
  779. if (this.mainTabCtrl.TabPages.Count == 0) {
  780. this.CreateNewWebBrowser();
  781. }
  782. }
  783. private void btnCloseAll_Click(object sender, System.EventArgs e)
  784. {
  785. for (int pagecount = this.mainTabCtrl.TabPages.Count;
  786. pagecount > 0;pagecount--) {
  787. this.mainTabCtrl.TabPages[pagecount-1].Dispose();
  788. }
  789. this.CreateNewWebBrowser();
  790. }
  791. #endregion
  792. #region Toolbar事件响应
  793. private void mainToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  794. {
  795. if(e.Button.Equals(this.tbb_New))
  796. {
  797. try
  798. {
  799. CreateNewWebBrowser();
  800. }
  801. catch{};
  802. }
  803. if(e.Button.Equals(this.tbb_Back))
  804. {
  805. try
  806. {
  807. GetCurrentBrowser().GoBack();
  808. }
  809. catch{};
  810. }
  811. if(e.Button.Equals(this.tbb_Forward))
  812. {
  813. try
  814. {
  815. GetCurrentBrowser().GoForward();
  816. }
  817. catch{};
  818. }
  819. if(e.Button.Equals(this.tbb_Stop))
  820. {
  821. GetCurrentBrowser().Stop();
  822. }
  823. if(e.Button.Equals(this.tbb_Refresh))
  824. {
  825. GetCurrentBrowser().Refresh();
  826. }
  827. if(e.Button.Equals(this.tbb_GoHome))
  828. {
  829. GetCurrentBrowser().GoHome();
  830. }
  831. }
  832. #endregion
  833. #region 菜单响应
  834. private void menu_FileNew_Click(object sender, System.EventArgs e)
  835. {
  836. CreateNewWebBrowser();
  837. }
  838. private void menu_ToolInternet_Click(object sender, System.EventArgs e)
  839. {
  840. this.InternetOptions();
  841. }
  842. private void menu_FileOpen_Click(object sender, System.EventArgs e)
  843. {
  844. if (GetCurrentBrowser().LocationURL != "")
  845. {
  846. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_OPEN,
  847. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  848. }
  849. }
  850. private void menu_FileSave_Click(object sender, System.EventArgs e)
  851. {
  852. if (GetCurrentBrowser().LocationURL != "") {
  853. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVEAS,
  854. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  855. }
  856. }
  857. private void menu_FileSaveAs_Click(object sender, System.EventArgs e)
  858. {
  859. if (GetCurrentBrowser().LocationURL != "") 
  860. {
  861. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVEAS,
  862. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  863. }
  864. }
  865. private void menu_FileProperty_Click(object sender, System.EventArgs e)
  866. {
  867. if(GetCurrentBrowser().LocationURL!="")
  868. {
  869. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_PROPERTIES,
  870. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  871. }
  872. }
  873. private void menu_EditFind_Click(object sender, System.EventArgs e)
  874. {
  875. this.Find();
  876. }
  877. private void menu_EditSelAll_Click(object sender, System.EventArgs e)
  878. {
  879. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_SELECTALL,
  880. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  881. }
  882. private void menu_EditCut_Click(object sender, System.EventArgs e)
  883. {
  884. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_CUT,
  885. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  886. }
  887. private void menu_EditCopy_Click(object sender, System.EventArgs e)
  888. {
  889. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY,
  890. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  891. }
  892. private void menuEditPaste_Click(object sender, System.EventArgs e)
  893. {
  894. GetCurrentBrowser().ExecWB(SHDocVw.OLECMDID.OLECMDID_PASTE,
  895. SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT);
  896. }
  897. private void menu_ViewSource_Click(object sender, System.EventArgs e)
  898. {
  899. this.ViewSource();
  900. }
  901. private void menu_AddToFavor_Click(object sender, System.EventArgs e)
  902. {
  903. }
  904. private void menu_HelpAbout_Click(object sender, System.EventArgs e)
  905. {
  906. MessageBox.Show("网页浏览器1.0","网页浏览器",MessageBoxButtons.OK,
  907. MessageBoxIcon.Information);
  908. }
  909. private void menu_FileExit_Click(object sender, System.EventArgs e)
  910. {
  911. this.Close();
  912. }
  913. #endregion
  914. #region TabControl 事件响应
  915. private void mainTabCtrl_SelectedIndexChanged(object sender, System.EventArgs e)
  916. {
  917. if (GetDocument() != null) {
  918. this.comboAddress.Text = GetDocument().url;
  919. }
  920. else
  921. {
  922. this.comboAddress.Text = "about:blank";
  923. }
  924. }
  925. private bool IsSolved = false;
  926. private void mainTabCtrl_DoubleClick(object sender, System.EventArgs e)
  927. {
  928. try
  929. {
  930. for (int i=this.mainTabCtrl.TabCount-1;i>=0;i--) 
  931. {
  932. Point pos = this.mainTabCtrl.PointToClient(Cursor.Position);
  933. if (this.mainTabCtrl.GetTabRect(i).Contains(pos)) 
  934. {
  935. this.mainTabCtrl.TabPages[i].Dispose();
  936. if(this.mainTabCtrl.TabCount == 0)
  937. CreateNewWebBrowser();
  938. IsSolved = true;
  939. return;
  940. }
  941. }
  942. }
  943. catch{}
  944. }
  945. private void MainForm_DoubleClick(object sender, System.EventArgs e)
  946. {
  947. if (this.mainTabCtrl.ClientRectangle.Contains(
  948. this.PointToClient(Cursor.Position))) {
  949. IsSolved = false;
  950. this.mainTabCtrl_DoubleClick(sender,e);
  951. if(!IsSolved)
  952. {
  953. CreateNewWebBrowser();
  954. }
  955. }
  956. }
  957. #endregion
  958. private void comboAddress_SelectedIndexChanged(object sender, System.EventArgs e)
  959. {
  960. }
  961. }
  962. }