| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 642 人关注过本帖
标题:[转载]如何对webbrowser和IE编程(五)
只看楼主 加入收藏
zy05320532
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:70
专家分:0
注 册:2006-5-30
结帖率:0
收藏
 问题点数:0 回复次数:0 
[转载]如何对webbrowser和IE编程(五)

自动化 Internet Explorer
自动化打开了开发基于web应用的世界。 它允许你使用VB或者VC定制成熟的应用。自动化的好处:通过属性和方法可以改变IE的外观;你可以提供诸如导航条等用户接口以便控制用户的导航。
自动化IE很容易。你建立一个简单的应用启动一个IE实例,然后使用控制webbrowser的途径- IWebBrowser2 接口来控制IE实例。
提醒

术语自动化(automation)真实的含义是通过自动化接口-- IDispatch.控制一个COM对象。但是在此是指控制IE的技术,你不需要直接通过IDispatch
使用VB
前面已经介绍了如何五分钟在VB中使用webbrowser来创建全功能的浏览器应用. 你也可以大致使用此时间用VB自动化IE。让我们开始。
启动一个Standard EXE 工程,选择References 菜单项. 引用对话框展开如Figure 6-19:
Figure 6-19. References dialog box.
滚动下拉,选中 Microsoft Internet Controls 检查框,点击OK 。加入一个命令按钮到窗体,命名为btnStart, 修改标题为 Start IE5. 然后双击加入click事件代码。
当用户点击Start IE5 按钮, 你想应用程序启动一个Internet Explorer 5实例. 先建立一个类型为 InternetExplorer 的全局变量. 命名为InternetExplorer1.
现在, 在btnStart的Click 事件中, 加入如果上一个实例没有创建就创建新IE实例的代码。你可以使用CreateObject 或者Vb的New 关键字.如下:
Set InternetExplorer1 = New InternetExplorer
该代码创建一个新实例,但是实例是隐藏的,要显示该实例,设定Visible 属性为 True, 如下:
InternetExplorer1.Visible = True
现在你需要导航到某个web页,你可以如下调用InternetExplorer 对象的Navigate方法, 如下:
InternetExplorer1.Navigate "http://www.microsoft.com/"
至此,整个Vb的自动化IE的源代码看起来如下:
Option Explicit
Dim InternetExplorer1 As InternetExplorer
Private Sub btnStart_Click()
' Only create a new instance of Internet Explorer
' if one hasn't already been created.
'
If Not InternetExplorer1 Is Nothing Then
Exit Sub
End If
Set InternetExplorer1 = New InternetExplorer

' Make Internet Explorer visible and navigate
' to Microsoft's home page.
'
InternetExplorer1.Visible = True
InternetExplorer1.Navigate "http://www.microsoft.com/"
End Sub
Private Sub Form_Load()
Set InternetExplorer1 = Nothing
End Sub
运行应用程序看到IE启动了! 新的IE实例将被启动导航到MS的主页。者不太困难,是不是?现在让我们加入一些实在的较酷的特征允许你控制你自己创建的IE实例。
首先保存工程为 VbAutoIE.vbp, 且保存你的表单 VbAutoIE.frm. 然后加入一些控制到你的表单,如图Figure 6-20. 这些允许你显示或者隐藏IE中不同的用户接口特征如地址栏、菜单条、状态条和工具条等。你也可以加入文字到状态条。
Figure 6-20. Visual Basic form with controls to customize the Internet Explorer user interface.
现在如下表设定每一个控件的属性如表6-8.创建4个选项组,每一个包含 一个显示和一个隐藏选项按钮如Figure 6-20.
Table 6-8. Control Properties for a Visual Basic Program Automating Internet Explorer
Control Properties
Frame1-4 Captions = "AddressBar", "MenuBar", "StatusBar ", and "ToolBar", respectively
Hide Option Buttons Caption = "Hide"; Index = 0; Value = False; Names = optAddrBar, optMenuBar, optStatusBar, and optToolBar, respectively
Show Option Buttons Caption = "Show"; Index = 1; Value = True; Names = optAddrBar, optMenuBar, optStatusBar, and optToolBar, respectively
Label Caption = "Status Text"
TextBox Name = txtStatusText. Remove the default text for the Text property
CommandButton Caption = "Change"; Name = btnChange
加入控制InternetExplorer 对象的代码控制浏览器的用户接口。看看清单6-1
Listing 6-1.
VbAutoIE.bas
Option Explicit
Dim InternetExplorer1 As InternetExplorer
Const HideBar = 0
Const ShowBar = 1
Private Sub btnChange_Click()
On Error Resume Next
InternetExplorer1.StatusText = txtStatusText.Text
End Sub
Private Sub btnStart_Click()
' Only create a new instance of Internet Explorer
' if one hasn't already been created.
'
If Not InternetExplorer1 Is Nothing Then
Exit Sub
End If

Set InternetExplorer1 = New InternetExplorer

' Set the user interface features to match the
' entries specified by the user.
'
If optAddrBar(ShowBar).Value = True Then
InternetExplorer1.AddressBar = True
Else
InternetExplorer1.AddressBar = False
End If

If optMenuBar(ShowBar).Value = True Then
InternetExplorer1.MenuBar = True
Else
InternetExplorer1.MenuBar = False
End If

If optToolBar(ShowBar).Value = True Then
InternetExplorer1.ToolBar = True
Else
InternetExplorer1.ToolBar = False
End If
If optStatusBar(ShowBar).Value = True Then
InternetExplorer1.StatusBar = True
Else
InternetExplorer1.StatusBar = False
End If

' Make Internet Explorer visible and navigate
' to Microsoft's home page.
'
InternetExplorer1.Visible = True
InternetExplorer1.Navigate "http://www.microsoft.com/"
End Sub
Private Sub Form_Load()
Set InternetExplorer1 = Nothing
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
InternetExplorer1.Quit
End Sub
Private Sub optAddrBar_Click(Index As Integer)
On Error Resume Next
InternetExplorer1.AddressBar = CBool(Index)
End Sub
Private Sub optMenuBar_Click(Index As Integer)
On Error Resume Next
InternetExplorer1.MenuBar = CBool(Index)
End Sub
Private Sub optStatusBar_Click(Index As Integer)
On Error Resume Next
InternetExplorer1.StatusBar = CBool(Index)
End Sub
Private Sub optToolBar_Click(Index As Integer)
On Error Resume Next
InternetExplorer1.ToolBar = Index
End Sub
在清单6-1, 当表单被装载, InternetExplorer1 对象设定为Nothing.当Start IE5 按钮被点击, 我们检查确信没有上一个实例启动,如果启动了我们直接返回。
如果上一实例没有启动,我们采用关键字New 创建一个新实例。然后我们检查选项组的状态.我们依据选项当前值进行IS属性的设置。然后设置Visible 属性为 True. 最后我们使用Navigate 方法导航到MS的主页.

搜索更多相关主题的帖子: webbrowser 
2006-05-31 21:05
快速回复:[转载]如何对webbrowser和IE编程(五)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021544 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved