【问题标题】:COM event handler in VBScriptVBScript 中的 COM 事件处理程序
【发布时间】:2011-08-14 16:40:28
【问题描述】:

我想捕捉 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()的区别?

提前致谢!

【问题讨论】:

    标签: events object com vbscript handler


    【解决方案1】:

    WScript.CreateObject 的第二个参数是事件处理函数中使用的前缀。要使其正常工作,请将您对 CreateObject 的调用更改为以下内容。

    Set CivicFactory = _
        WScript.CreateObject("LocationDisp.CivicAddressReportFactory", _
            "CivicFactory_")
    

    WScript.CreateObject 和 CreateObject 的区别在于 WScript.CreateObject 支持事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多