Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:8k
源码类别:

C#编程

开发平台:

Others

  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. namespace MyIEApp
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class Form1 : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.StatusBar statusBar1;
  15. private AxSHDocVw.AxWebBrowser MyWebBrowser;
  16. private System.Windows.Forms.ComboBox MyAddressBox;
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.Panel panel1;
  19. private System.Windows.Forms.ToolBarButton toolBarButton1;
  20. private System.Windows.Forms.ToolBarButton toolBarButton2;
  21. private System.Windows.Forms.ToolBarButton toolBarButton3;
  22. private System.Windows.Forms.ToolBarButton toolBarButton4;
  23. private System.Windows.Forms.ToolBarButton toolBarButton5;
  24. private System.Windows.Forms.ToolBarButton toolBarButton6;
  25. private System.Windows.Forms.ToolBar toolBar1;
  26. private System.ComponentModel.IContainer components;
  27. public Form1()
  28. {
  29. //
  30. // Windows 窗体设计器支持所必需的
  31. //
  32. InitializeComponent();
  33. //
  34. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  35. //
  36. }
  37. /// <summary>
  38. /// 清理所有正在使用的资源。
  39. /// </summary>
  40. protected override void Dispose( bool disposing )
  41. {
  42. if( disposing )
  43. {
  44. if (components != null) 
  45. {
  46. components.Dispose();
  47. }
  48. }
  49. base.Dispose( disposing );
  50. }
  51. #region Windows Form Designer generated code
  52. /// <summary>
  53. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  54. /// 此方法的内容。
  55. /// </summary>
  56. private void InitializeComponent()
  57. {
  58. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  59. this.statusBar1 = new System.Windows.Forms.StatusBar();
  60. this.MyWebBrowser = new AxSHDocVw.AxWebBrowser();
  61. this.MyAddressBox = new System.Windows.Forms.ComboBox();
  62. this.label1 = new System.Windows.Forms.Label();
  63. this.panel1 = new System.Windows.Forms.Panel();
  64. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  65. this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
  66. this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
  67. this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
  68. this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
  69. this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
  70. this.toolBar1 = new System.Windows.Forms.ToolBar();
  71. ((System.ComponentModel.ISupportInitialize)(this.MyWebBrowser)).BeginInit();
  72. this.panel1.SuspendLayout();
  73. this.SuspendLayout();
  74. // 
  75. // statusBar1
  76. // 
  77. this.statusBar1.Location = new System.Drawing.Point(0, 333);
  78. this.statusBar1.Name = "statusBar1";
  79. this.statusBar1.Size = new System.Drawing.Size(496, 16);
  80. this.statusBar1.TabIndex = 4;
  81. // 
  82. // MyWebBrowser
  83. // 
  84. this.MyWebBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
  85. this.MyWebBrowser.Enabled = true;
  86. this.MyWebBrowser.Location = new System.Drawing.Point(0, 0);
  87. this.MyWebBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("MyWebBrowser.OcxState")));
  88. this.MyWebBrowser.Size = new System.Drawing.Size(496, 349);
  89. this.MyWebBrowser.TabIndex = 0;
  90. this.MyWebBrowser.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.MyWebBrowser_StatusTextChange);
  91. // 
  92. // MyAddressBox
  93. // 
  94. this.MyAddressBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  95. | System.Windows.Forms.AnchorStyles.Right)));
  96. this.MyAddressBox.ItemHeight = 12;
  97. this.MyAddressBox.Location = new System.Drawing.Point(56, 8);
  98. this.MyAddressBox.Name = "MyAddressBox";
  99. this.MyAddressBox.Size = new System.Drawing.Size(434, 20);
  100. this.MyAddressBox.TabIndex = 5;
  101. this.MyAddressBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MyAddressBox_KeyPress);
  102. // 
  103. // label1
  104. // 
  105. this.label1.Location = new System.Drawing.Point(5, 13);
  106. this.label1.Name = "label1";
  107. this.label1.Size = new System.Drawing.Size(48, 16);
  108. this.label1.TabIndex = 4;
  109. this.label1.Text = "地址:";
  110. // 
  111. // panel1
  112. // 
  113. this.panel1.Controls.Add(this.MyAddressBox);
  114. this.panel1.Controls.Add(this.label1);
  115. this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
  116. this.panel1.Location = new System.Drawing.Point(0, 41);
  117. this.panel1.Name = "panel1";
  118. this.panel1.Size = new System.Drawing.Size(496, 40);
  119. this.panel1.TabIndex = 5;
  120. // 
  121. // toolBarButton1
  122. // 
  123. this.toolBarButton1.ImageIndex = 0;
  124. this.toolBarButton1.Text = "后退";
  125. // 
  126. // toolBarButton2
  127. // 
  128. this.toolBarButton2.ImageIndex = 1;
  129. this.toolBarButton2.Text = "前进";
  130. // 
  131. // toolBarButton3
  132. // 
  133. this.toolBarButton3.ImageIndex = 2;
  134. this.toolBarButton3.Text = "停止";
  135. // 
  136. // toolBarButton4
  137. // 
  138. this.toolBarButton4.ImageIndex = 3;
  139. this.toolBarButton4.Text = "刷新";
  140. // 
  141. // toolBarButton5
  142. // 
  143. this.toolBarButton5.ImageIndex = 4;
  144. this.toolBarButton5.Text = "主页";
  145. // 
  146. // toolBarButton6
  147. // 
  148. this.toolBarButton6.ImageIndex = 5;
  149. this.toolBarButton6.Text = "搜索";
  150. // 
  151. // toolBar1
  152. // 
  153. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  154. this.toolBarButton1,
  155. this.toolBarButton2,
  156. this.toolBarButton3,
  157. this.toolBarButton4,
  158. this.toolBarButton5,
  159. this.toolBarButton6});
  160. this.toolBar1.ButtonSize = new System.Drawing.Size(49, 35);
  161. this.toolBar1.DropDownArrows = true;
  162. this.toolBar1.Font = new System.Drawing.Font("华文行楷", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  163. this.toolBar1.Location = new System.Drawing.Point(0, 0);
  164. this.toolBar1.Name = "toolBar1";
  165. this.toolBar1.ShowToolTips = true;
  166. this.toolBar1.Size = new System.Drawing.Size(496, 41);
  167. this.toolBar1.TabIndex = 1;
  168. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  169. // 
  170. // Form1
  171. // 
  172. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  173. this.ClientSize = new System.Drawing.Size(496, 349);
  174. this.Controls.Add(this.panel1);
  175. this.Controls.Add(this.statusBar1);
  176. this.Controls.Add(this.toolBar1);
  177. this.Controls.Add(this.MyWebBrowser);
  178. this.Name = "Form1";
  179. this.Text = "模拟IE浏览器";
  180. ((System.ComponentModel.ISupportInitialize)(this.MyWebBrowser)).EndInit();
  181. this.panel1.ResumeLayout(false);
  182. this.ResumeLayout(false);
  183. }
  184. #endregion
  185. /// <summary>
  186. /// 应用程序的主入口点。
  187. /// </summary>
  188. [STAThread]
  189. static void Main() 
  190. {
  191. Application.Run(new Form1());
  192. }
  193. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  194. {
  195. try 
  196. {
  197. switch (toolBar1.Buttons.IndexOf (e.Button ))
  198. {
  199. case 0: 
  200. MyWebBrowser.GoBack (); //后退
  201. break;
  202. case 1:
  203. MyWebBrowser.GoForward (); //前进
  204. break;
  205. case 2:
  206. MyWebBrowser.Stop (); //停止
  207. break;
  208. case 3:
  209. MyWebBrowser.Refresh (); //刷新
  210. break;
  211. case 4:
  212. MyWebBrowser.GoHome (); //主页
  213. break;
  214. case 5:
  215. MyWebBrowser.GoSearch (); //搜索
  216. break;
  217. }
  218. }
  219. catch{}
  220. }
  221. private void MyNavigate()
  222. {
  223. object anObject = new object ();
  224. try
  225. {
  226. this.MyWebBrowser.Navigate(MyAddressBox.Text , ref anObject, ref anObject, ref anObject, ref anObject);
  227. catch{}
  228. AddAdress();
  229. statusBar1.Text = MyWebBrowser.LocationName ;
  230. }
  231. private void AddAdress()
  232. {
  233. int AdressIndex = MyAddressBox.FindStringExact (MyAddressBox.Text );
  234. if (AdressIndex < 0) //如果地址不在ComboBox中,则添加地址
  235. {
  236. MyAddressBox.Items.Add (MyAddressBox.Text);
  237. }
  238. }
  239. private void MyAddressBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  240. {
  241. if ( e.KeyChar == (char)13 ) //回车
  242. {
  243. MyNavigate();
  244. e.Handled = true;
  245. }
  246. }
  247. private void MyWebBrowser_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
  248. {
  249. statusBar1.Text = e.text;
  250. }
  251. }
  252. }