Delphi开发ActiveForm,FEvents=nil的解决

使用Delphi开发一个ActiveForm,其他功能都没问题,但自定义事件不执行,单步跟踪发现EventSinkChanged事件未执行,所以FEvents=nil。以为是创建工程哪个选项选错了,重建了好几次工程,问题依旧。在网上看到有人给出解决办法,试了一下,果然好了。问题在于JS中绑定ActiveForm事件的方式上。

我原来是这样绑定事件的:

 

<OBJECT id="testid"  
  classid="clsid:62949CA7-D723-4D98-AD9A-0DFEBCE5C14F"  
  codebase="ActiveFormProj1.ocx#version=1,0,0,0"  
  width="350" height="250" align="center" hspace="0" vspace="0"
>  
</OBJECT>  
<script type="text/javascript">
  document.getElementById("testid").attachEvent("OnClick", function(){  alert("OnMyEvent fired!"); });
</script>

 这样绑定事件,在ActiveForm中不会触发EventSinkChanged事件,所以FEvents=nil,导致自定义事件不会正确触发。改成下面这样就可以了:

<OBJECT id="testid"  
  classid="clsid:62949CA7-D723-4D98-AD9A-0DFEBCE5C14F"  
  codebase="ActiveFormProj1.ocx#version=1,0,0,0"  
  width="350" height="250" align="center" hspace="0" vspace="0"
>  
</OBJECT>  
<script type="text/javascript" event="OnMyEvent" for="testid">  
  alert("OnMyEvent fired!");  
</script>

 再试了一下,这样绑定也可以:

function onEventHandler(arg1, arg2){
    // do something
}

window.onload = function(){
    var yourActiveXObject = document.getElementById('YourObjectTagID');
    if(typeof(yourActiveXObject) === 'undefined' || yourActiveXObject === null){
        alert('Unable to load ActiveX');
        return;
    }

    // attach events
    var status = yourActiveXObject.attachEvent('EventName', onEventHandler);
}

 

 

参考资料:

http://stackoverflow.com/questions/150814/how-to-handle-an-activex-event-in-javascript

 

你可能感兴趣的:(Delphi)