C#做浏览器源程序

开发 后端
这里介绍C#做浏览器源程序,掌握了COM组件在Visual C#使用方法,就可以利用Visual 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. }  

 

编译源程序和编译后的执行程序的运行界面在经过如下命令编译后,就可以得到可以自己的浏览器了csc /t:winexe /r:AxSHDocVw.dll /r:SHDocVw.dll /r:system.dll/r:system.windows.forms.dll /r:system.drawing.dll brower.cs

总结
至此一个功能相对完备的“浏览器”就算完成了,其实用Visual C#做浏览器源程序的过程,也就是Visual C#中使用COM组件的过程。掌握了COM组件在Visual C#使用方法,就可以利用Visual C#编写出功能更强大,适应性更强的软件来,但编写的过程又十分的简单。

【编辑推荐】

  1. 浅谈C# Connection对象
  2. C#实现PrintPage方法
  3. 利用Visual C#和C#语言特性
  4. C#管道技术学习经验
  5. 概述C#复合控件构建
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-11 13:48:11

C# ConfigDl

2009-08-31 14:54:35

C#对象浏览器

2009-09-03 17:49:59

C#浏览器开发

2009-09-03 18:55:08

C#判断浏览器

2009-09-03 19:00:15

C#判断浏览器

2009-09-03 17:54:04

C#开发浏览器

2009-08-24 18:06:36

源程序代码C#读取XML文件

2009-09-04 11:03:32

C#文件浏览器

2009-09-04 09:27:48

C#调用浏览器

2009-08-18 13:59:21

C#多标签浏览器

2011-06-10 16:44:17

Qt 浏览器

2009-09-04 10:05:16

C#调用浏览器浏览器的原理

2009-08-20 10:48:05

C#做Screen C

2013-11-22 09:58:36

2012-09-24 14:43:51

C#网络协议C

2012-03-19 17:25:22

2012-03-20 11:41:18

海豚浏览器

2012-03-20 11:31:58

移动浏览器

2012-03-20 11:07:08

2012-06-21 15:38:02

猎豹浏览器
点赞
收藏

51CTO技术栈公众号