Flash与外部程序之间的通信

  flash as3中有一个类ExternalInterface,该类是在flash.external包中,外部程序与as3通信使用ExternalInterface。“ExternalInterface 类是外部 API,在 ActionScript 和 Flash Player 的容器之间实现直接通讯的应用程序编程接口,例如,含有 JavaScript 的 HTML 页。 推荐对所有 JavaScript 与 ActionScript 之间的通信使用 ExternalInterface”。这句是从AS3API摘取,说明了ExternalInterface类的作用。

  addCallback(functionName:String,closure:Function):void ,addCallback是ExternalInterface类的静态方法,将 AS中的方法注册为可让外部容器调用,另一个静态方法call(functionName:String,... arguments):*则是调用由外部容器公开的函数的。

  下用以下代码判断flash是否位于提供外部接口的容器中

if (ExternalInterface.available){}

  注册可让外部容器调用的方法

if (ExternalInterface.available) { //point 1
try {

ExternalInterface.addCallback("CreateBitmap", CreateBitmap);//字符串CreateBitmap是外部程序调用的方法名,第二个参数是方法引用
ExternalInterface.addCallback("ResizeBitmap",ResizeBitmap);
ExternalInterface.addCallback("RemoveBitmap",RemoveBitmpa);

} catch (error:SecurityError) {
if(stage!=null){
MessageBox2.show(stage,200,200,"A SecurityError occurred",error.message,true);
}else{
MessageBox2.show(this,200,200,"A SecurityError occurred",error.message,false);
}


} catch (error:Error) {

if(stage!=null){
MessageBox2.show(stage,200,200,"An Error occurred",error.message,true);
}else{
MessageBox2.show(this,200,200,"An Error occurred",error.message,false);
}

}

} else {

//output.appendText("External interface不可用。");

}


public function CreateBitmap():void{
            this.visible=false;
            if(bitmapData==null){
                bitmapData=new BitmapData(this._DisplayWidth,this._DisplayHeight,true,0x00000000);
                bitmapData.draw(this);
                bitmap=new Bitmap(bitmapData);
                this.parent.addChild(bitmap);
                bitmap.x=this.x;
                bitmap.y=this.y;
            }
        }
        
        
        public function ResizeBitmap(bNumber,bheight:Number):void{
            if(bitmap!=null&&bwidth>0&&bheight>0){
                bitmap.width=bwidth;
                bitmap.height=bheight;
                bitmap.x=stage.stageWidth>bitmap.width?(stage.stageWidth-bitmap.width)/2:0;
                bitmap.y=stage.stageHeight>bitmap.height?(stage.stageHeight-bitmap.height)/2:0
            }
        }
        
        public function RemoveBitmpa():void{
            
            if(bitmap!=null&&this.parent.contains(bitmap)){
                this.parent.removeChild(bitmap);
            }
            if(bitmapData!=null)bitmapData=null;
            if(bitmap!=null)bitmap=null;
            if(this.visible==false)this.visible=true;
        }

注册好外部容器可调用的方法后,外部就可以调用这些方法了,下面是在delphi中调用

procedure TPaperSizeFrm.ResizeFlash;
var
s:WideString;
begin
s:=Format('<invoke name="ResizeBitmap"><arguments><number>%s</number><number>%s</number></arguments></invoke>',[edtWidth.Text,edtHeight.Text]);
flashPreView.CallFunction(s)
end;




原文地址:https://www.cnblogs.com/skybdemq/p/2299187.html