Visual C#是微軟推出的新一代程式開發語言,Visual C#實現許多功能是通過調用.Net架構為其中所有.Net程式開發語言提供的一個公用的軟體包——.Net FrameWork SDK。在這個軟體包中提供了大量並且十分豐富的類庫,可以說,沒有這個軟體開發包,Visual C#就寸步難行,無法編寫哪怕一個功能十分的程式。但這樣也會出現一個問題,如果在.Net FrameWork SDK軟體包中沒有涉及到的功能,而在其他的第三方的COM組件中卻提供了,那麼這些組件是否可以被Visual C#使用。答案是:直接使用是不可以的,但這些COM組件經過一定轉換後就可以。這種轉換就是非受管代碼(Unmanaged Code)到受管代碼(Managed Code)的轉換。因為這些COM組件一般都是非受管代碼(Unmanaged Code),而編譯Visual C#檔案時候要使用的類庫卻只能為受管代碼(Managed Code),這就是說要在Visual C#中使用那些非受管程式碼群組件,就必須把這些非受管程式碼群組件轉換成受管程式碼群組件。在.Net架構中專門提供了一個程式“Aximp.exe”來實現由COM組件到WinForm組件的轉換。那麼這個檔案在哪裡?假設你安裝.Net FrameWork SDK在“C”盤,那麼在“C:\Program Files\Microsoft.NET\FrameworkSDK\Bin”目錄中就可以找到這個檔案。如果你安裝.Net FrameWork SDK在其他目錄或者磁碟,依照上述的目錄順序就可以找到這個檔案了。 下面用Visual C#來做一個“瀏覽器”,看看在Visual C#是如何使用COM組件的。 一.本文程式設計和啟動並執行軟體環境 (1).微軟公司視窗2000伺服器版 (2)..Net FrameWork SDK Beta 2 二.程式設計的思路以及關鍵步驟的解決方案 (1).把轉換COM組件為WinForm組件: 其實實現這種轉換十分的簡單,我們知道微軟Web瀏覽器COM組件名稱為“shdocvw.dll”,由於我們使用的是視窗2000,所以這個檔案是存放在“c:\winnt\system32”目錄中,如果你使用的是視窗98或者是視窗Me,那麼此組件存放的位置是“c:\windows\system”。“Aximp.exe”檔案後面有許多的參數,你可以通過“Aximp /?”來瞭解,在本文中只使用下列簡單的命令就可成功轉換: Aximp c:\winnt\system32\shdocvw.dll 運行上述命令後就可以實現轉換,並在目前的目錄中產生“SHDocVw.dll”和“AxSHDocVw.dll”二個檔案。具體如: 圖01:轉換COM組件為WinForm組件 (2).在程式中使用轉換後組件: 在“AxSHDocVw.dll”中定義了命名空間“AxSHDocVw”,在此命名空間中定義了一個“AxWebBrowser”組件,這個組件中有若干個方法和屬性,Visual C#就是通過這些方法和屬性來實現瀏覽器的一些準系統的。使用此瀏覽器組件和使用其他的WinForm組件的過程是一樣的,首先要匯入命名空間,然後在程式中繼承此命名空間中定義的瀏覽器組件,最後設定這個繼承後的組件的屬性和方法。具體如下: < I > .匯入命名空間,具體代碼如下: using AxSHDocVw ; < II> . 繼承此命名空間中定義的瀏覽器組件,具體代碼如下: private AxWebBrowser axWebBrowser1 ; (3).通過轉換後組件來實現瀏覽器的一些準系統: 瀏覽器的主要功能就是能夠到指定的地址瀏覽資訊,當然在具體的瀏覽中還有一些基本的功能,譬如:“前進”、“後退”、“停止”、“重新整理”、“首頁”等,這些功能都可以通過“AxWebBrowser”組件來實現。下面就來具體介紹: < I > .瀏覽指定的地址: 在程式中,網址是填寫在組件“textbox1”中的,“瀏覽指定地址”功能是通過程式的按鈕“轉到”來實現的。下面是按鈕“轉到”按動後的程式碼: 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 ; } < II > .瀏覽器的“前進”、“後退”、“停止”、“重新整理”、“首頁”功能: 在“AxWebBrowser”組件中對這些功能都有一個具體的方法來與之對應,具體如下面代碼: private void toolBar1_ButtonClick ( object sender , ToolBarButtonClickEventArgs e ) { //瀏覽器中的“後退” if ( e.Button == tb1 ) { axWebBrowser1.GoBack ( ) ; } //瀏覽器中的“前進” if ( e.Button == tb2 ) { axWebBrowser1.GoForward ( ) ; } //瀏覽器中的“停止” if ( e.Button == tb3 ) { axWebBrowser1.Stop ( ) ; } //瀏覽器中的“重新整理” if ( e.Button == tb4 ) { axWebBrowser1.Refresh ( ) ; } //瀏覽器中的“首頁” if ( e.Button == tb5 ) { axWebBrowser1.GoHome ( ) ; } } < III > .當然掌握了上面的知識,你就可以用Visual C#做出一個基本的瀏覽器了,但下面這些也是不可缺少的,因為下面這些代碼將使得你做的瀏覽器更專業。下面代碼的作用是使得瀏覽介面隨著表單的變化而變化,按鈕和文字框也要隨著表單的變化而變化。 button1.Anchor = ( AnchorStyles.Top | AnchorStyles.Right ) ; //定位“轉到”按鈕組件與表單的上、右邊框保持一致 textBox1.Anchor = ( ( AnchorStyles.Top | AnchorStyles.Left ) | AnchorStyles.Right ) ; //定位地址文字框組件與表單的上、左、右邊框保持一致 axWebBrowser1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom ) | AnchorStyles.Left ) | AnchorStyles.Right ) ; //定位瀏覽器組件與表單的上、下、左、右邊框保持一致 三.來源程式代碼(brower.cs) 瞭解有了上面的這些,就可以比較容易編寫一個屬於自己的瀏覽器了,下面是用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 ) ; } //初始化表單中的各個組件 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 = "前進" ; 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 ) ; //定位“轉到”按鈕組件與表單的上、右邊框保持一致 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 = "轉到" ; 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 ( ) ) ; } //實現瀏覽器主要功能 private void toolBar1_ButtonClick ( object sender , ToolBarButtonClickEventArgs e ) { //瀏覽器中的“後退” if ( e.Button == tb1 ) { axWebBrowser1.GoBack ( ) ; } //瀏覽器中的“前進” 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 ; } } 四.編譯來源程式和編譯後的執行程式的運行介面 在經過如下命令編譯後,就可以得到可以自己的瀏覽器了 csc /t:winexe /r:AxSHDocVw.dll /r:SHDocVw.dll /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll brower.cs 圖02:用Visual C#做的“瀏覽器”的運行介面 五.總結 至此一個功能相對完備的“瀏覽器”就算完成了,其實用Visual C#做“瀏覽器”的過程,也就是Visual C#中使用COM組件的過程。掌握了COM組件在Visual C#使用方法,就可以利用Visual C#編寫出功能更強大,適應性更強的軟體來,但編寫的過程又十分的簡單。 |