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
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