C#做瀏覽器源程序
C#做瀏覽器源程序
了解C#源程序代碼,就可以比較容易編寫一個(gè)屬于自己的瀏覽器了,下面是用Visual C#做瀏覽器源程序代碼,他具備了IE瀏覽器的一些常用的功能。
- using System ;
- using System.Drawing ;
- using System.Collections ;
- using System.ComponentModel ;
- using System.Windows.Forms ;
- using System.Data ;
- using AxSHDocVw ;
- public class Form1 : Form
- {
- private ToolBar toolBar1 ;
- private ToolBarButton tb1 ;
- private ToolBarButton tb2 ;
- private ToolBarButton tb3 ;
- private ToolBarButton tb4 ;
- private ToolBarButton tb5 ;
- private Label label1 ;
- private TextBox textBox1 ;
- private Button button1 ;
- private AxWebBrowser axWebBrowser1 ;
- private System.ComponentModel.Container components = null ;
- public Form1 ( )
- {
- InitializeComponent ( ) ;
- }
- //清除程序中使用過的資源
- protected override void Dispose ( bool disposing )
- {
- if ( disposing )
- {
- if ( components != null )
- {
- components.Dispose ( ) ;
- }
- }
- base.Dispose ( disposing ) ;
- }
- //初始化窗體中的各個(gè)組件
- private void InitializeComponent ( )
- {
- tb1 = new ToolBarButton ( ) ;
- tb2 = new ToolBarButton ( ) ;
- tb3 = new ToolBarButton ( ) ;
- toolBar1 = new ToolBar ( ) ;
- tb4 = new ToolBarButton ( ) ;
- tb5 = new ToolBarButton ( ) ;
- button1 = new Button ( ) ;
- textBox1 = new TextBox ( ) ;
- axWebBrowser1 = new AxWebBrowser ( ) ;
- label1 = new Label ( ) ;
- ( ( System.ComponentModel.ISupportInitialize )
( this.axWebBrowser1 ) ).BeginInit ( ) ;- this.SuspendLayout ( ) ;
- tb1.Text = "后退" ;
- tb2.Text = "前進(jìn)" ;
- tb3.Text = "停止" ;
- tb4.Text = "刷新" ;
- tb5.Text = "主頁" ;
- toolBar1.Appearance = ToolBarAppearance.Flat ;
- toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
- //在工具欄中加入按鈕
- toolBar1.Buttons.Add ( tb1 ) ;
- toolBar1.Buttons.Add ( tb2 ) ;
- toolBar1.Buttons.Add ( tb3 ) ;
- toolBar1.Buttons.Add ( tb4 ) ;
- toolBar1.Buttons.Add ( tb5 ) ;
- toolBar1.DropDownArrows = true ;
- toolBar1.Name = "toolBar1" ;
- toolBar1.ShowToolTips = true ;
- toolBar1.Size = new System.Drawing.Size ( 612 , 39 ) ;
- toolBar1.TabIndex = 0 ;
- toolBar1.ButtonClick += new ToolBarButtonClickEventHandler ( toolBar1_ButtonClick ) ;
- //定位“轉(zhuǎn)到”按鈕組件與窗體的上、右邊框保持一致
- button1.Anchor = ( AnchorStyles.Top | AnchorStyles.Right ) ;
- button1.DialogResult = DialogResult.OK ;
- button1.Location = new System.Drawing.Point ( 544 , 45 ) ;
- button1.Name = "button1" ;
- button1.Size = new System.Drawing.Size ( 40 , 23 ) ;
- button1.TabIndex = 3 ;
- button1.Text = "轉(zhuǎn)到" ;
- button1.Click += new System.EventHandler ( button1_Click ) ;
- //定位地址文本框組件與窗體的上、左、右邊框保持一致
- textBox1.Anchor = ( ( AnchorStyles.Top | AnchorStyles.Left )
- | AnchorStyles.Right ) ;
- textBox1.Location = new System.Drawing.Point ( 64 , 47 ) ;
- textBox1.Name = "textBox1" ;
- textBox1.Size = new System.Drawing.Size ( 464 , 21 ) ;
- textBox1.TabIndex = 2 ;
- textBox1.Text = "" ;
- //定位瀏覽器組件與窗體的上、下、左、右邊框保持一致
- axWebBrowser1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom )
- | AnchorStyles.Left )
- | AnchorStyles.Right ) ;
- axWebBrowser1.Enabled = true ;
- axWebBrowser1.Location = new System.Drawing.Point ( 0 , 72 ) ;
- axWebBrowser1.Size = new System.Drawing.Size ( 608 , 358 ) ;
- axWebBrowser1.TabIndex = 4 ;
- label1.Location = new System.Drawing.Point ( 16 , 48 ) ;
- label1.Name = "label1" ;
- label1.Size = new System.Drawing.Size ( 48 , 16 ) ;
- label1.TabIndex = 1 ;
- label1.Text = "地址:" ;
- this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
- this.ClientSize = new System.Drawing.Size ( 612 , 433 ) ;
- this.Controls.Add ( axWebBrowser1 ) ;
- this.Controls.Add ( button1 ) ;
- this.Controls.Add ( textBox1 ) ;
- this.Controls.Add ( label1 ) ;
- this.Controls.Add ( toolBar1 ) ;
- this.FormBorderStyle = FormBorderStyle.FixedSingle ;
- this.Name = "Form1" ;
- this.Text = "visual C#做瀏覽器" ;
- ( ( System.ComponentModel.ISupportInitialize )
( this.axWebBrowser1 ) ).EndInit ( ) ;- this.ResumeLayout ( false ) ;
- }
- static void Main ( )
- {
- Application.Run ( new Form1 ( ) ) ;
- }
- //實(shí)現(xiàn)瀏覽器主要功能
- private void toolBar1_ButtonClick
( object sender , ToolBarButtonClickEventArgs e )- {
- //瀏覽器中的“后退”
- if ( e.Button == tb1 )
- {
- axWebBrowser1.GoBack ( ) ;
- }
- //瀏覽器中的“前進(jìn)”
- if ( e.Button == tb2 )
- {
- axWebBrowser1.GoForward ( ) ;
- }
- //瀏覽器中的“停止”
- if ( e.Button == tb3 )
- {
- axWebBrowser1.Stop ( ) ;
- }
- //瀏覽器中的“刷新”
- if ( e.Button == tb4 )
- {
- axWebBrowser1.Refresh ( ) ;
- }
- //瀏覽器中的“主頁”
- if ( e.Button == tb5 )
- {
- axWebBrowser1.GoHome ( ) ;
- }
- }
- //瀏覽指定的Web地址
- private void button1_Click ( object sender , System.EventArgs e )
- {
- System.Object nullObject = 0 ;
- string str = "" ;
- System.Object nullObjStr = str ;
- Cursor.Current = Cursors.WaitCursor ;
- axWebBrowser1.Navigate ( textBox1.Text , ref nullObject ,
ref nullObjStr , ref nullObjStr , ref nullObjStr ) ;- Cursor.Current = Cursors.Default ;
- }
- }
編譯源程序和編譯后的執(zhí)行程序的運(yùn)行界面在經(jīng)過如下命令編譯后,就可以得到可以自己的瀏覽器了csc /t:winexe /r:AxSHDocVw.dll /r:SHDocVw.dll /r:system.dll/r:system.windows.forms.dll /r:system.drawing.dll brower.cs
總結(jié)
至此一個(gè)功能相對完備的“瀏覽器”就算完成了,其實(shí)用Visual C#做瀏覽器源程序的過程,也就是Visual C#中使用COM組件的過程。掌握了COM組件在Visual C#使用方法,就可以利用Visual C#編寫出功能更強(qiáng)大,適應(yīng)性更強(qiáng)的軟件來,但編寫的過程又十分的簡單。
【編輯推薦】