學(xué)習(xí)用VB制作瀏覽器

字號(hào):

自己做瀏覽器?有沒(méi)有搞錯(cuò)?不要說(shuō)像IE這樣的龐然大物,就是小巧的Opera,我們大多數(shù)普通人也決計(jì)搞不出來(lái)。但如果你的機(jī)器里裝有VB5.0專業(yè)版,那么事情就好辦多了,想試試嗎?那好,Let`s go!
    程序的主角是一個(gè)ActiveX控件:WebBrowser。當(dāng)然,缺省狀態(tài)下VB的工具箱中并沒(méi)有它,我們得手工加入,方法是:右擊工具箱,在出現(xiàn)的快捷菜單中選擇“部件...”,確保在彈出的對(duì)話框中選中“控件”標(biāo)簽,找到Microsoft Internet Controls,在它前面的小框中打鉤,然后確定。此時(shí)你會(huì)發(fā)現(xiàn)工具箱中多了兩個(gè)小圖標(biāo),其中,地球圖標(biāo)代表的控件正是我們需要的WebBrowser。
    由于許多人對(duì)WebBrowser控件不是很熟悉,VB的幫助中也沒(méi)有有關(guān)它的內(nèi)容(反正我沒(méi)有找到),因此有必要介紹一下它的屬性、方法和事件,限于篇幅,我們只涉及程序中用到的:
    屬性:LocationURL 返回控件顯示W(wǎng)EB頁(yè)面的URL。
    方法:Navigate 轉(zhuǎn)移到指定的URL或打開指定HTML文件。
    事件:1.DownloadBegin 下載操作開時(shí)觸發(fā)。
    2.DownloadComplete 下載操作完成、終止或失敗時(shí)觸發(fā)。
    3.ProgressChange WebBrowser控件跟蹤下載操作的過(guò)程,并定期觸發(fā)此事件。其語(yǔ)法為:Sub WebBrowser_ProgressChange (ByVal Progress As Long, ByVal ProgressMax As Long)。Progress變?cè)钱?dāng)前已下載的數(shù)據(jù)總量,ProgressMax變?cè)菍⒁螺d的數(shù)據(jù)總量。
    4.TitleChange 當(dāng)前文檔標(biāo)題改變時(shí)觸發(fā)
    除了WebBrowser控件外,程序還需要一個(gè)Label控件:Label1;一個(gè)ComboBox控件:combo1,用來(lái)顯示URL地址;一個(gè)StatusBar控件:StatusBar1;一個(gè)ProgressBar控件:ProgressBar1,用來(lái)顯示下載進(jìn)度(StatusBar控件和ProgressBar控件是ActiveX控件Microsoft Windows Common Controls5.0的成員,加入工具箱的方法同WebBrowser控件),這些控件的屬性值都用缺省值。
    以下是程序清單:
    Option Explicit
    Private Sub Form_Load()
    Me.Caption =“My Explorer”
    Label1.Caption = “URL”
    Combo1.Text = “”
    Combo1.Top = Label1.Height
    Combo1.Left = 0
    WebBrowser1.Top = Combo1.Top + Combo1.Height
    WebBrowser1.Left = 0
    Form_Resize
    StatusBar1.Style = sbrSimple
    ProgressBar1.ZOrder
    End Sub
    Private Sub Form_Resize()
    On Error GoTo a
    Combo1.Width = Form1.Width - 100
    WebBrowser1.Width = Combo1.Width
    WebBrowser1.Height = Form1.Height - Combo1.Height - 1000
    ProgressBar1.Top = Me.Height - StatusBar1.Height - 330
    ProgressBar1.Left = 0.25 * StatusBar1.Width
    ProgressBar1.Width = 0.75 * Me.Width - 250
    a:
    End Sub
    Private Sub Combo1_Click()
    `轉(zhuǎn)到指定網(wǎng)址
    WebBrowser1.Navigate Combo1.Text
    End Sub
    Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim i As Long
    Dim existed As Boolean
    If KeyCode = 13 Then
    If Left(Combo1.Text, 7) <> “http://”Then
    Combo1.Text = “http://”+ Combo1.Text
    End If
    WebBrowser1.Navigate Combo1.Text
    For i = 0 To Combo1.ListCount - 1
    If Combo1.List(i) = Combo1.Text Then
    existed = True
    Exit For
    Else
    existed = False
    End If
    Next
    If Not existed Then
    Combo1.AddItem (Combo1.Text)
    End If
    End If
    End Sub
    Private Sub WebBrowser1_DownloadBegin()
    `下載開始時(shí)狀態(tài)欄顯示“Now Linking...”
    StatusBar1.SimpleText = “Now Linking...”
    End Sub
    Private Sub WebBrowser1_DownloadComplete()
    `下載完成時(shí)狀態(tài)欄顯示“Link Finished”
    StatusBar1.SimpleText = “Link Finished”
    ProgressBar1.Value = 0
    End Sub
    Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long,
    ByVal ProgressMax As Long)
    `下載進(jìn)行時(shí)進(jìn)度條變化
    If ProgressMax = 0 Then Exit Sub
    ProgressBar1.Max = ProgressMax
    If Progress <> -1 And Progress <= ProgressMax Then
    ProgressBar1.Value = Progress
    End If
    End Sub
    Private Sub WebBrowser1_TitleChange(ByVal Text As String)
    Combo1.Text = WebBrowser1.LocationURL
    End Sub