VBScript 中的 COM 事件处理程序
我想捕获 NewCivicAddressReport 事件,这意味着我需要实现事件处理程序。谁能解释为什么嵌入在 html 页面中的 VBScript 代码可以工作,但 VBS 文件却不能?
这是可以在 CivicFactory_NewCivicAddressReport() 函数中处理 NewCivicAddressReport 事件的 html 页面。我想这是因为事件处理程序命名约定。如果我错了请纠正我。
<!-- Civic address Location report factory object -->
<object id="CivicFactory"
classid="clsid:2A11F42C-3E81-4ad4-9CBE-45579D89671A"
type="application/x-oleobject">
</object>
<script language="vbscript">
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "New civic address report!"
End Function
Sub OnLoadPage()
CivicFactory.ListenForReports(1000)
End Sub
Sub DisplayStatus(status)
MsgBox "status displayed"
End Sub
</script>
下面是不起作用的 VBS 文件 - 事件处理函数似乎永远不会被调用。
Dim CivicFactory
Set CivicFactory = WScript.CreateObject("LocationDisp.CivicAddressReportFactory")
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "Location changed!"
keepSleeping=false
End Function
CivicFactory.ListenForReports(1000)
dim keepSleeping
keepSleeping=true
while keepSleeping
WScript.Sleep 200
wend
顺便问一下,谁能告诉我创建对象的两种方法和 WScript.CreateObject() 之间的区别?
提前致谢!
I want to catch the NewCivicAddressReport event which means I need to implement the event handler. Can anyone explain why the VBScript code embedded in html page works but the VBS file doesn't?
Here is the html page where NewCivicAddressReport events can be handled in the CivicFactory_NewCivicAddressReport() function. I suppose it is because of the event handler naming convention. Correct me if I'm wrong.
<!-- Civic address Location report factory object -->
<object id="CivicFactory"
classid="clsid:2A11F42C-3E81-4ad4-9CBE-45579D89671A"
type="application/x-oleobject">
</object>
<script language="vbscript">
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "New civic address report!"
End Function
Sub OnLoadPage()
CivicFactory.ListenForReports(1000)
End Sub
Sub DisplayStatus(status)
MsgBox "status displayed"
End Sub
</script>
And below is the VBS file which doesn't work - the event handler function seems never gets called.
Dim CivicFactory
Set CivicFactory = WScript.CreateObject("LocationDisp.CivicAddressReportFactory")
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "Location changed!"
keepSleeping=false
End Function
CivicFactory.ListenForReports(1000)
dim keepSleeping
keepSleeping=true
while keepSleeping
WScript.Sleep 200
wend
By the way, can anyone tell me the difference between the two ways of creating an object: and WScript.CreateObject()?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WScript.CreateObject< 的第二个参数/code>
是事件处理函数中使用的前缀。要使其正常工作,请将对 CreateObject 的调用更改为以下内容。
WScript.CreateObject 和 CreateObject 之间的区别在于 WScript.CreateObject 支持事件。
The second argument for
WScript.CreateObject
is the prefix used in your event-handling Functions. For it to work, change your call to CreateObject to the following.The difference between WScript.CreateObject and CreateObject is that WScript.CreateObject supports events.