Make your own browser? Is there any mistake? Not to mention behemoths like IE, even the compact Opera, most of us ordinary people will never be able to do it. But if you have VB5.0 professional version installed on your machine, then things will be much easier. Do you want to try it? Well, let`s go!
The protagonist of the program is an ActiveX control: WebBrowser. Of course, it is not included in the VB toolbox by default. We have to add it manually. The method is: right-click the toolbox, select "Parts..." in the shortcut menu that appears, and make sure it is selected in the pop-up dialog box. "Controls" tab, find Microsoft Internet Controls, check the small box in front of it, and then OK. At this point you will find two more small icons in the toolbox. Among them, the control represented by the globe icon is the WebBrowser we need.
Since many people are not very familiar with the WebBrowser control, and there is no content about it in the VB help (I didn't find it anyway), it is necessary to introduce its properties, methods and events. Due to space limitations, we only cover those used in the program. of:
Property: LocationURL Returns the URL of the WEB page displayed by the control.
Method: Navigate transfers to the specified URL or opens the specified HTML file.
Event: 1. DownloadBegin Fires when the download operation starts.
2. DownloadComplete Fires when a download operation completes, terminates, or fails.
3. The ProgressChange WebBrowser control tracks the progress of the download operation and triggers this event periodically. The syntax is: Sub WebBrowser_ProgressChange (ByVal Progress As Long, ByVal ProgressMax As Long). The Progress argument is the total amount of data currently downloaded, and the ProgressMax argument is the total amount of data to be downloaded.
4. TitleChange is triggered when the title of the current document changes.
In addition to the WebBrowser control, the program also needs a Label control: Label1; a ComboBox control: combo1, used to display the URL address; a StatusBar control: StatusBar1; a ProgressBar control: ProgressBar1, used to display the download progress (StatusBar control and ProgressBar control are The ActiveX control is a member of Microsoft Windows Common Controls 5.0. The method of adding it to the toolbox is the same as the WebBrowser control). The property values of these controls use default values.
Here is the program listing:
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() `Go to the specified URL 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() `When the download starts, the status bar displays "Now Linking..." StatusBar1.SimpleText = "Now Linking..." End Sub |
Private Sub WebBrowser1_DownloadComplete() `When the download is completed, the status bar displays "Link Finished" StatusBar1.SimpleText = "Link Finished" ProgressBar1.Value = 0 End Sub |
Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long) `The progress bar changes when the download is in progress 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 |