http://www.16sheji8.cn/
对上述窗体添加如下代码即可实现对Internet Explorer的调用与控制,
Dim WithEvents ieView As InternetExplorer
'在通用模块中定义Internet Explorer对象
Private Sub Form_Load()
cmdForward.Visible = False
cmdBackward.Visible = False
cmdCloseView.Visible = False
'窗体调入时隐藏Internet Explorer控制按钮
End Sub
Private Sub cmdView_Click()http://www.16sheji8.cn/
Dim txtAddress As String
On Error Resume Next '设置错误陷阱
Set ieView = GetObject("", "InternetExplorer.Application")
ieView.Visible = True
'创建Internet Explorer对象的一个实例,并设为可见
txtAddress = Trim(Text1.Text)
ieView.Navigate txtAddress, navNoReadFromCache
'按text1控件的内容进行浏览http://www.16sheji8.cn/
cmdView.Enabled = False
cmdExit.Enabled = False
cmdForward.Visible = True
cmdBackward.Visible = True
cmdCloseView.Visible = True
'使浏览、退出命令钮无效,使IE控制命令钮可见
End Sub
Private Sub cmdForward_Click()
On Error Resume Next '设置错误陷阱
ieView.GoForward
End Sub
Private Sub cmdBackward_Click()
On Error Resume Next '设置错误陷阱
ieView.GoBack
End Sub
Private Sub cmdCloseView_Click()
On Error Resume Next '设置错误陷阱
ieView.Quit
End Subhttp://www.16sheji8.cn/
Private Sub ieView_OnQuit()
Set ieView = Nothing
'消除ieView对象
cmdForward.Visible = False
cmdBackward.Visible = False
cmdCloseView.Visible = False
cmdView.Enabled = True
cmdExit.Enabled = True
'恢复初始按钮状态
End Sub
Private Sub cmdExit_Click()
End
End Sub
程序启动后"向前"、"向后"、"关闭浏览窗口"等命令钮不可见,在文本框内输入相应的URL地址,单击"浏览"按钮即可调入Internet Explorer进行浏览,此时,"向前"、"向后"、"关闭浏览窗口"命令钮转为可见,从而实现对浏览器的控制,单击"关闭浏览窗口"命令按钮可关闭当前打开的Internet Explorer窗口,此时窗口又还原为初始模样,可再次输入新的地址进行浏览。
三、 注意问题 http://www.16sheji8.cn/
1. 必须在最上层通用模块中定义Internet Explorer对象,并且将其定义为WithEvents类型以便响应由Active X组件触发的事件;
2. 浏览时需用下述语句创建Internet Explorer对象实例,并设为可见:
Set ieView = GetObject("", "InternetExplorer.Application")
ieView.Visible = True
退出时须消除该对象以释放内存,即
Set ieView = Nothing
否则会因大量占用系统资源导致系统不稳定。
3. 调用和控制Internet Explorer过程中会产生各种错误,如在浏览器历史记录为空时,执行"向前"、"向后"操作就会产生OlE自动化错误,必须在"浏览"、"向前"、"向后"、"关闭浏览窗口"等命令按钮相应程序模块开始处加入错误捕获语句:
On Error Resume Nexthttp://www.16sheji8.cn/
以检测和处理类似错误,上述程序在Windows98、VB 5.0环境下调试通过。