如何用C#做浏览器源程序

发布时间:2021-12-02 11:33:07 作者:iii
来源:亿速云 阅读:115

这篇文章主要讲解了“如何用C#做浏览器源程序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何用C#做浏览器源程序”吧!

C#做浏览器源程序

了解C#源程序代码,就可以比较容易编写一个属于自己的浏览器了,下面是用Visual C#做浏览器源程序代码,他具备了IE浏览器的一些常用的功能。

  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 AxSHDocVw ;  

  8. public class Form1 : Form  

  9. {  

  10. private ToolBar toolBar1 ;  

  11. private ToolBarButton tb1 ;  

  12. private ToolBarButton tb2 ;  

  13. private ToolBarButton tb3 ;  

  14. private ToolBarButton tb4 ;  

  15. private ToolBarButton tb5 ;  

  16. private Label label1 ;  

  17. private TextBox textBox1 ;  

  18. private Button button1 ;  

  19. private AxWebBrowser axWebBrowser1 ;  

  20. private System.ComponentModel.Container components = null ;  

  21. public Form1 ( )  

  22. {  

  23. InitializeComponent ( ) ;  

  24. }  

  25. //清除程序中使用过的资源  

  26. protected override void Dispose ( bool disposing )  

  27. {  

  28. if ( disposing )  

  29. {  

  30. if ( components != null )  

  31. {  

  32. components.Dispose ( ) ;  

  33. }  

  34. }  

  35. base.Dispose ( disposing ) ;  

  36. }  

  37. //初始化窗体中的各个组件  

  38. private void InitializeComponent ( )  

  39. {  

  40. tb1 = new ToolBarButton ( ) ;  

  41. tb2 = new ToolBarButton ( ) ;  

  42. tb3 = new ToolBarButton ( ) ;  

  43. toolBar1 = new ToolBar ( ) ;  

  44. tb4 = new ToolBarButton ( ) ;  

  45. tb5 = new ToolBarButton ( ) ;  

  46. button1 = new Button ( ) ;  

  47. textBox1 = new TextBox ( ) ;  

  48. axWebBrowser1 = new AxWebBrowser ( ) ;  

  49. label1 = new Label ( ) ;  

  50. ( ( System.ComponentModel.ISupportInitialize ) 
    ( this.axWebBrowser1 ) ).BeginInit ( ) ;  

  51. this.SuspendLayout ( ) ;  

  52.  

  53. tb1.Text = "后退" ;  

  54. tb2.Text = "前进" ;  

  55. tb3.Text = "停止" ;  

  56. tb4.Text = "刷新" ;  

  57. tb5.Text = "主页" ;  

  58.  

  59. toolBar1.Appearance = ToolBarAppearance.Flat ;  

  60. toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;  

  61. //在工具栏中加入按钮  

  62. toolBar1.Buttons.Add ( tb1 ) ;  

  63. toolBar1.Buttons.Add ( tb2 ) ;  

  64. toolBar1.Buttons.Add ( tb3 ) ;  

  65. toolBar1.Buttons.Add ( tb4 ) ;  

  66. toolBar1.Buttons.Add ( tb5 ) ;  

  67. toolBar1.DropDownArrows = true ;  

  68. toolBar1.Name = "toolBar1" ;  

  69. toolBar1.ShowToolTips = true ;  

  70. toolBar1.Size = new System.Drawing.Size ( 612 , 39 ) ;  

  71. toolBar1.TabIndex = 0 ;  

  72. toolBar1.ButtonClick += new ToolBarButtonClickEventHandler ( toolBar1_ButtonClick ) ;  

  73. //定位“转到”按钮组件与窗体的上、右边框保持一致  

  74. button1.Anchor = ( AnchorStyles.Top | AnchorStyles.Right ) ;  

  75. button1.DialogResult = DialogResult.OK ;  

  76. button1.Location = new System.Drawing.Point ( 544 , 45 ) ;  

  77. button1.Name = "button1" ;  

  78. button1.Size = new System.Drawing.Size ( 40 , 23 ) ;  

  79. button1.TabIndex = 3 ;  

  80. button1.Text = "转到" ;  

  81. button1.Click += new System.EventHandler ( button1_Click ) ;  

  82. //定位地址文本框组件与窗体的上、左、右边框保持一致  

  83. textBox1.Anchor = ( ( AnchorStyles.Top | AnchorStyles.Left )  

  84. | AnchorStyles.Right ) ;  

  85. textBox1.Location = new System.Drawing.Point ( 64 , 47 ) ;  

  86. textBox1.Name = "textBox1" ;  

  87. textBox1.Size = new System.Drawing.Size ( 464 , 21 ) ;  

  88. textBox1.TabIndex = 2 ;  

  89. textBox1.Text = "" ;  

  90. //定位浏览器组件与窗体的上、下、左、右边框保持一致  

  91. axWebBrowser1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom )  

  92. | AnchorStyles.Left )  

  93. | AnchorStyles.Right ) ;  

  94. axWebBrowser1.Enabled = true ;  

  95. axWebBrowser1.Location = new System.Drawing.Point ( 0 , 72 ) ;  

  96. axWebBrowser1.Size = new System.Drawing.Size ( 608 , 358 ) ;  

  97. axWebBrowser1.TabIndex = 4 ;  

  98.  

  99. label1.Location = new System.Drawing.Point ( 16 , 48 ) ;  

  100. label1.Name = "label1" ;  

  101. label1.Size = new System.Drawing.Size ( 48 , 16 ) ;  

  102. label1.TabIndex = 1 ;  

  103. label1.Text = "地址:" ;  

  104.  

  105. this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;  

  106. this.ClientSize = new System.Drawing.Size ( 612 , 433 ) ;  

  107.  

  108. this.Controls.Add ( axWebBrowser1 ) ;  

  109. this.Controls.Add ( button1 ) ;  

  110. this.Controls.Add ( textBox1 ) ;  

  111. this.Controls.Add ( label1 ) ;  

  112. this.Controls.Add ( toolBar1 ) ;  

  113. this.FormBorderStyle = FormBorderStyle.FixedSingle ;  

  114. this.Name = "Form1" ;  

  115. this.Text = "visual C#做浏览器" ;  

  116. ( ( System.ComponentModel.ISupportInitialize ) 
    ( this.axWebBrowser1 ) ).EndInit ( ) ;  

  117. this.ResumeLayout ( false ) ;  

  118.  

  119. }  

  120. static void Main ( )  

  121. {  

  122. Application.Run ( new Form1 ( ) ) ;  

  123. }  

  124. //实现浏览器主要功能  

  125. private void toolBar1_ButtonClick 
    ( object sender , ToolBarButtonClickEventArgs e )  

  126. {  

  127. //浏览器中的“后退”  

  128. if ( e.Button == tb1 )  

  129. {  

  130. axWebBrowser1.GoBack ( ) ;  

  131. }  

  132. //浏览器中的“前进”  

  133. if ( e.Button == tb2 )  

  134. {  

  135. axWebBrowser1.GoForward ( ) ;  

  136. }  

  137. //浏览器中的“停止”  

  138. if ( e.Button == tb3 )  

  139. {  

  140. axWebBrowser1.Stop ( ) ;  

  141. }  

  142. //浏览器中的“刷新”  

  143. if ( e.Button == tb4 )  

  144. {  

  145. axWebBrowser1.Refresh ( ) ;  

  146. }  

  147. //浏览器中的“主页”  

  148. if ( e.Button == tb5 )  

  149. {  

  150. axWebBrowser1.GoHome ( ) ;  

  151. }  

  152.  

  153. }  

  154. //浏览指定的Web地址  

  155. private void button1_Click ( object sender , System.EventArgs e )  

  156. {  

  157. System.Object nullObject = 0 ;  

  158. string str = "" ;  

  159. System.Object nullObjStr = str ;  

  160. Cursor.Current = Cursors.WaitCursor ;  

  161. axWebBrowser1.Navigate ( textBox1.Text , ref nullObject , 
    ref nullObjStr , ref nullObjStr , ref nullObjStr ) ;  

  162. Cursor.Current = Cursors.Default ;  

  163. }  

  164. }  

编译源程序和编译后的执行程序的运行界面在经过如下命令编译后,就可以得到可以自己的浏览器了

感谢各位的阅读,以上就是“如何用C#做浏览器源程序”的内容了,经过本文的学习后,相信大家对如何用C#做浏览器源程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 如何用敏捷方法做测试?
  2. 如何用Jmeter做压力测试

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:VB.NET中抓取网页乱码怎么办

下一篇:tk.Mybatis插入数据获取Id怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》