监控网页中元素的事件

Internet开发 同时被 2 个专栏收录
24 篇文章 0 订阅
24 篇文章 0 订阅

对编写脚本熟悉的读者都知道页面中可以添加脚本响应元素的事件,例如超链接的OnClick事件,图片的OnMouseMove事件,我们也可以使VB程序响应这些事件。下面是一个VB响应页面中按钮的Click事件的代码:
首先建立一个新工程,在Form1中加入一个Webbrowser控件,然后在Form1中加入以下代码:

Option Explicit

Public Sub Some_Procedure()
MsgBox "你点击了按钮."
End Sub

Private Sub Form_Load()
'下载空页面
WebBrowser1.Navigate2 "about:blank"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'建立事件响应类
Dim cfForward As clsForward

'定义在浏览器中显示的HTML代码,其中包含一个按钮btnMyButton
Dim sHTML As String
sHTML = "

This is some text.

"
sHTML = sHTML & "

And here is a button.

"
sHTML = sHTML & " "

'将HTML代码写入浏览器
WebBrowser1.Document.body.innerHTML = sHTML

'将事件响应类连接到页面的按钮btnMyButton上
Set cfForward = New clsForward
cfForward.Set_Destination Me, "Some_Procedure"
WebBrowser1.Document.All("btnMyButton").onclick = cfForward
End Sub

向工程中添加一个Class Module,Class Module的Name属性设定为clsForward,在clsForward中添加以下代码:

Option Explicit

Dim oObject As Object
Dim sMethod As String
Dim bInstantiated As Boolean

Private Sub Class_Initialize()
bInstantiated = False
End Sub

Public Sub Set_Destination(oInObject As Object, sInMethod As String)
Set oObject = oInObject
sMethod = sInMethod
bInstantiated = True
End Sub

Public Sub My_Default_Method()
If bInstantiated Then
CallByName oObject, sMethod, VbMethod
End If
End Sub

This is some text.

"
sHTML = sHTML & "

And here is a button.

"
sHTML = sHTML & " "

'将HTML代码写入浏览器
WebBrowser1.Document.body.innerHTML = sHTML

'将事件响应类连接到页面的按钮btnMyButton上
Set cfForward = New clsForward
cfForward.Set_Destination Me, "Some_Procedure"
WebBrowser1.Document.All("btnMyButton").onclick = cfForward
End Sub

向工程中添加一个Class Module,Class Module的Name属性设定为clsForward,在clsForward中添加以下代码:

Option Explicit

Dim oObject As Object
Dim sMethod As String
Dim bInstantiated As Boolean

Private Sub Class_Initialize()
bInstantiated = False
End Sub

Public Sub Set_Destination(oInObject As Object, sInMethod As String)
Set oObject = oInObject
sMethod = sInMethod
bInstantiated = True
End Sub

Public Sub My_Default_Method()
If bInstantiated Then
CallByName oObject, sMethod, VbMethod
End If
End Sub

在运行前,你需要吧My_Default_Method设置为默认过程,方法是:在clsForward模块的代码窗口中将光标定位到My_Default_Method中,然后选择菜单 Tools | Procedure Attributes。然后在弹出窗口中点击advanced按钮,然后在procedure id下拉框中选择[Default]


运行程序,点击Webbrowser中的“Click this button”按钮。程序就会弹出消息框提示“你点击了按钮.”

 

www.applevb.com

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值