![]() 圖01:COM組件的轉換過程 在成功轉換過以后,就要利用轉換后的這二個文件,用VB.NET來做一個屬于自己的瀏覽器。 三. 本文程序設計和運行的軟件環境: (1).微軟公司視窗2000服務器版 (2)..Net FrameWork SDK Beta 2 四. 程序設計的思路以及關鍵步驟的解決方法: (1).在VB.NET中使用瀏覽器組件: 在轉換而成的"AxSHDocVw.dll"和"SHDocVw.dll"中定義了命名空間"AxSHDocVw",在此命名空間中封裝了一個"AxWebBrowser"組件,這個組件中有若干個方法和屬性,VB.NET就是通過這些方法和屬性來實現瀏覽器的一些基本功能的。使用此瀏覽器組件和使用其他的WinForm組件的過程是一樣的,首先要導入命名空間,然后在程序中繼承此命名空間中定義的瀏覽器組件,最后設定這個繼承后的組件的屬性和方法。具體如下: < I > .導入命名空間,具體代碼如下: Imports AxSHDocVw < II > . 繼承此命名空間中定義的瀏覽器組件,具體代碼如下: Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser (2).VB.NET實現瀏覽器的一些基本功能: 要用VB.NET做一個屬于自己的瀏覽器的,就必須掌握用VB.NET是如何實現瀏覽器的一些基本功能的,我們所講的瀏覽器的基本功能其實也就是瀏覽器工具欄上的那些功能按鈕,譬如:前進、后退、主頁、停止等,其中瀏覽器中最為重要,也是比較難實現的,就是如何在給定網址后,通過"轉到"按鈕,瀏覽指定的網頁,下面就來具體介紹一下。 < I > .瀏覽指定網頁: 在下列的代碼中我們是通過TextBox1文本框組件來放置指定的網址的,"Button1"按鈕組件就是定義的"轉到"按鈕,下列就是用VB.NET實現瀏覽指定網頁的程序代碼: Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click Dim nullObject As System.Object = 0 Dim str As String = "" Dim nullObjStr As System.Object = str Cursor.Current = Cursors.WaitCursor AxWebBrowser1.Navigate ( TextBox1.Text , nullObject , nullObjStr , nullObjStr , nullObjStr ) Cursor.Current = Cursors.Default End Sub < II > .VB.NET實現瀏覽器的基本功能: 在本文的程序中,我們也是仿照IE瀏覽器的大致式樣,同樣設定了一個工具欄(ToolBar1),并且在工具欄中放置了一些按鈕(ToolBarButton1至ToolBarButton5),通過這些按鈕來實現瀏覽器的前進、后退等基本功能,下列是實現這些功能的代碼: Private Sub ToolBar1_ButtonClick ( ByVal sender As System.Object , ByVal e As ToolBarButtonClickEventArgs ) Handles ToolBar1.ButtonClick '實現瀏覽器中的"后退"功能 If e.Button Is ToolBarButton1 Then AxWebBrowser1.GoBack ( ) End If '實現瀏覽器中的"前進"功能 If e.Button Is ToolBarButton2 Then AxWebBrowser1.GoForward ( ) End If '實現瀏覽器中的"主頁"功能 If e.Button Is ToolBarButton3 Then AxWebBrowser1.GoHome ( ) End If '實現瀏覽器中的"刷新"功能 If e.Button Is ToolBarButton4 Then AxWebBrowser1.Refresh ( ) End If '實現瀏覽器中的"停止"功能 If e.Button Is ToolBarButton5 Then AxWebBrowser1.Stop ( ) End If End Sub < III > .其實掌握了上面的這些知識,一個基本的瀏覽器的框架大致也就出來了,但由于瀏覽器頁面大小是經常變化,如果你不設定窗體中的組件是跟隨窗體大小變化而隨之變化的話,這個做出來瀏覽器也就顯得不那么專業,所以下面這些畫龍點睛的代碼也是不可缺少的。下面代碼的作用是使得瀏覽界面上的組件隨著窗體的變化而變化,即窗體中按鈕和文本框等要隨著窗體的變化而變化。 '定位"地址"文本框組件與窗體的下、左、右邊框保持一致 TextBox1.Anchor = ( ( ( AnchorStyles.Top Or AnchorStyles.Bottom ) _ Or AnchorStyles.Left ) _ Or AnchorStyles.Right ) '定位"轉到"按鈕組件與窗體的上、右邊框保持一致 Button1.Anchor = ( AnchorStyles.Top Or AnchorStyles.Right ) '定位"瀏覽器"組件與窗體的上、下、左、右邊框保持一致 AxWebBrowser1.Anchor = ( ( ( AnchorStyles.Top Or AnchorStyles.Bottom ) _ Or AnchorStyles.Left ) _ Or AnchorStyles.Right ) 五. 用VB.NET做瀏覽器的源程序代碼(IE.vb): 在了解了上面的這些要點后,可以得到用VB.NET做瀏覽器的完整代碼,具體如下: Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports AxSHDocVw '導入程序中使用到的命名空間 Public Class Form1 Inherits Form Public Sub New ( ) MyBase.New ( ) '初始化窗體中的各個組件 InitializeComponent ( ) End Sub '清除程序中使用的各種資源 Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean ) If disposing Then If Not ( components Is Nothing ) Then components.Dispose ( ) End If End If MyBase.Dispose ( disposing ) End Sub Friend WithEvents ToolBar1 As ToolBar Friend WithEvents ToolBarButton1 As ToolBarButton Friend WithEvents ToolBarButton2 As ToolBarButton Friend WithEvents ToolBarButton3 As ToolBarButton Friend WithEvents ToolBarButton4 As ToolBarButton Friend WithEvents ToolBarButton5 As ToolBarButton Friend WithEvents Label1 As Label Friend WithEvents TextBox1 As TextBox Friend WithEvents Button1 As Button Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser Private components As System.ComponentModel.Container Private Sub InitializeComponent ( ) TextBox1 = New TextBox ( ) ToolBarButton1 = New ToolBarButton ( ) ToolBarButton2 = New ToolBarButton ( ) ToolBarButton3 = New ToolBarButton ( ) ToolBarButton4 = New ToolBarButton ( ) ToolBarButton5 = New ToolBarButton ( ) Label1 = New Label ( ) ToolBar1 = New ToolBar ( ) Button1 = New Button ( ) AxWebBrowser1 = New AxSHDocVw.AxWebBrowser ( ) CType ( AxWebBrowser1 , System.ComponentModel.ISupportInitialize ).BeginInit ( ) SuspendLayout ( ) '定位"地址"文本框組件與窗體的下、左、右邊框保持一致 TextBox1.Anchor = ( ( ( AnchorStyles.Top Or AnchorStyles.Bottom ) _ Or AnchorStyles.Left ) _ Or AnchorStyles.Right ) TextBox1.Location = New Point ( 48 , 40 ) TextBox1.Name = "TextBox1" TextBox1.Size = New Size ( 296 , 21 ) TextBox1.TabIndex = 2 TextBox1.Text = "" ToolBarButton1.Text = "向后" ToolBarButton3.Text = "主頁" ToolBarButton2.Text = "向前" ToolBarButton4.Text = "刷新" ToolBarButton5.Text = "停止" Label1.Location = New Point ( 8 , 48 ) Label1.Name = "Label1" Label1.Size = New Size ( 48 , 23 ) Label1.TabIndex = 1 Label1.Text = "地址:" '以下是在工具欄中加入按鈕 ToolBar1.Buttons.Add ( ToolBarButton1 ) ToolBar1.Buttons.Add ( ToolBarButton2 ) ToolBar1.Buttons.Add ( ToolBarButton3 ) ToolBar1.Buttons.Add ( ToolBarButton4 ) ToolBar1.Buttons.Add ( ToolBarButton5 ) ToolBar1.DropDownArrows = True ToolBar1.Name = "ToolBar1" ToolBar1.ShowToolTips = True ToolBar1.Size = New Size ( 400 , 38 ) ToolBar1.TabIndex = 0 '定位"轉到"按鈕組件與窗體的上、右邊框保持一致 Button1.Anchor = ( AnchorStyles.Top Or AnchorStyles.Right ) Button1.Location = New Point ( 352 , 40 ) Button1.Name = "Button1" Button1.Size = New Size ( 40 , 23 ) Button1.TabIndex = 3 Button1.Text = "轉到" '定位"瀏覽器"組件與窗體的上、下、左、右邊框保持一致 AxWebBrowser1.Anchor = ( ( ( AnchorStyles.Top Or AnchorStyles.Bottom ) _ Or AnchorStyles.Left ) _ Or AnchorStyles.Right ) AxWebBrowser1.Enabled = True AxWebBrowser1.Location = New Point ( 0 , 64 ) AxWebBrowser1.Size = New Size ( 400 , 240 ) AxWebBrowser1.TabIndex = 4 Me.AutoScaleBaseSize = New Size ( 6 , 14 ) Me.ClientSize = New Size ( 400 , 301 ) '在窗體上加入組件 Me.Controls.Add ( AxWebBrowser1 ) Me.Controls.Add ( Button1 ) Me.Controls.Add ( TextBox1 ) Me.Controls.Add ( Label1 ) Me.Controls.Add ( ToolBar1 ) Me.Name = "Form1" Me.Text = "VB.NET做個性化瀏覽器" CType ( Me.AxWebBrowser1 , System.ComponentModel.ISupportInitialize ).EndInit ( ) Me.ResumeLayout ( False ) End Sub '實現瀏覽器的功能 Private Sub ToolBar1_ButtonClick ( ByVal sender As System.Object , ByVal e As ToolBarButtonClickEventArgs ) Handles ToolBar1.ButtonClick '實現瀏覽器中的"后退"功能 If e.Button Is ToolBarButton1 Then AxWebBrowser1.GoBack ( ) End If '實現瀏覽器中的"前進"功能 If e.Button Is ToolBarButton2 Then AxWebBrowser1.GoForward ( ) End If '實現瀏覽器中的"主頁"功能 If e.Button Is ToolBarButton3 Then AxWebBrowser1.GoHome ( ) End If '實現瀏覽器中的"刷新"功能 If e.Button Is ToolBarButton4 Then AxWebBrowser1.Refresh ( ) End If '實現瀏覽器中的"停止"功能 If e.Button Is ToolBarButton5 Then AxWebBrowser1.Stop ( ) End If End Sub '實現"轉到"按鈕功能 Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click Dim nullObject As System.Object = 0 Dim str As String = "" Dim nullObjStr As System.Object = str Cursor.Current = Cursors.WaitCursor AxWebBrowser1.Navigate ( TextBox1.Text , nullObject , nullObjStr , nullObjStr , nullObjStr ) Cursor.Current = Cursors.Default End Sub End Class Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module 六. 編譯命令和運行界面: 在經過如下命令編譯后,就可以得到可以自己的瀏覽器了 vbc /t:winexe /r:AxSHDocVw.dll /r:SHDocVw.dll /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll ie.cs 下圖是此程序運行的界面: ![]() 圖02:用VB.NET做成的瀏覽器的運行界面 七. Visual Stduio .Net的實現方法: 隨著Visual Stduio .Net的正式版的推出,很多程序員都已經開始使用。其實用Visual Stduio .Net來實現上面的這些步驟,就顯得非常的容易,因為上面的很多步驟,在Visual Stduio .Net中都被掩蓋了起來,也就是說很多步驟都讓Visual Stduio .Net給你在不知不覺中做出來了。譬如COM組件的轉換,在你選擇新建了一個項目后,按動工具欄,點擊右鍵,選擇"自定義工具欄",具體如下: ![]() 圖03:自定義工具欄的操作界面 就可以得到下面界面: ![]() 圖04:在Visual Stdio .Net中使用COM組件 選擇"Microsoft Web 瀏覽器",你就會發現在Windows窗體的工具欄上面多了一個新的組件,這個組件就是Visual Stdio .Net給你轉換好的可以在VB.NET中使用的瀏覽器組件。此時你就可以在新建的窗體上面加入此組件,這時Visual Stdio .Net就給你完成了COM組件的轉換,并且也為你創建一個瀏覽器的組件。此時就可以根據上文中介紹的要點來實現瀏覽器的一些基本功能,具體的實現方法和上面的完全一樣,這里就不詳細介紹了。 八. 總結: 用VB.NET做瀏覽器的過程其實也就是用VB.NET進行COM組件編程的過程,在本文中,按照COM組件的轉換,COM組件的使用的步驟一步步的說明。雖然隨著Visual Sduio .Net正式版的推出,我們在以后的編程中使用的平臺更多的傾向于他,但由于Visual Sduio .Net在實際的操作過程中掩蓋了許多的底層的操作,這其實對我們更深入的掌握.Net程序開發是不利的,這也就是本文為什么一開始沒有直接從Visual Sduio .Net編程開始。其實對一個高手來說,.Net方面的編程,用什么工具都是次要的,關鍵是要掌握其中的來龍去脈,你說對么? 關于作者 馬金虎(筆名:王天),武漢大學計算機系畢業,現就職于滁州供電局信息中心,微軟2001-2002年度MVP。愛好做一些計算機相關的研究,發表過許多計算機相關文章。您可以通過QQ:23895859同作者聯系。 (出處:賽迪網) |
溫馨提示:喜歡本站的話,請收藏一下本站!