代码
package com.magReader
{
import flash.events.Event;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
//import flash.system.System;
import mx.controls.SWFLoader;
/**
* UIComponent designed specifically to load FlashPaper documents
* and size them correctly in the available area.
*/
public class FlashPaperLoader extends SWFLoader
{
/**
* The id of the FlashPaperLoader.swf proxy that will be used
* for communication pyurposes.
*/
public static const FLASH_PAPER_LOADED : String = "flashPaperLoaded";
public static const FLASH_CONNERROR : String = "flashConnError";
private var sendFlashConn:LocalConnection;
private var recieveFlashConn:LocalConnection;
/**
* The name of the application that is loading in the FlashPaperLoader.swf
* file. This name must correspond to the "id" attribute in the object tag
* and the "name" attribute in the embed tag when the Flex client is embedded
* into the html page.
*/
/**
* Constructor
*/
public function FlashPaperLoader()
{
//source = "app-storage:/data/fpHolder.swf";
sendFlashConn=new LocalConnection();
recieveFlashConn = new LocalConnection();
recieveFlashConn.client=this;
sendFlashConn.addEventListener(StatusEvent.STATUS, onStatus);
sendFlashConn.allowDomain("*");
recieveFlashConn.allowDomain("*");
sendConn();
}
public function sendConn():void
{
try
{
recieveFlashConn.connect("_flexloader");
} catch (error:ArgumentError) {
trace("Can't connect...the connection name is already being used by another SWF");
onConnError();
return;
}
}
private function onStatus(result:StatusEvent) :void{
trace (result.level == "error"?"Operation failed":"Operation succeeded");
}
//连接源出错
private function onConnError():void
{
//errUnload();
var e:Event=new Event(FlashPaperLoader.FLASH_CONNERROR);
dispatchEvent( e );
}
// =================================================================
// Expose methods that are proxied from FlashPaperLoader.swf - Call
// JavaScript methods that the FlashPaperLoader.swf file picks up
// and passes to the loaded FlashPaper document.
// =================================================================
public function setSize( width:Number, height:Number ):void
{
trace("=========setPaperSize=============");
sendFlashConn.send("_flashpaperloader","setPaperSize",width,height);
}
/**
* 文档加载成功提示
* */
public function fpLoaded():void
{
trace("reveice fpLoaded message!! this.width = " + this.width + " this.height" + this.height);
//setSize(parent.width,parent.height);
var e:Event=new Event(FlashPaperLoader.FLASH_PAPER_LOADED);
dispatchEvent( e );
//this.visible=true;
}
/**
* 设置缩放
* */
public function setZoom(value:Object):void
{
if (this.visible)
{
sendFlashConn.send("_flashpaperloader","setCurrentZoom",value);
}
}
// override protected function updateDisplayList( unscaledWidth:Number,
// unscaledHeight:Number ):void
// {
// if ( contentHolder )
// {
// // Adjust values so the FlashPaper document is displayed correctly
// contentHolder.scaleX = 1.0;
// contentHolder.scaleY = 1.0;
// contentHolder.x = 0;
// contentHolder.y = 0;
//
// contentHolder.scrollRect = new Rectangle( 0, 0, unscaledWidth, unscaledHeight );
//
// // When the content has loaded, call the setSize method so that the
// // FlashPaper document sizes right in the available area
// if ( Loader( contentHolder ).content )
// {
// setSize( unscaledWidth, unscaledHeight );
// //this.setFocus();
// }
// }
// }
//卸载此swf
public function unload():void
{
if(sendFlashConn != null)
{
sendFlashConn.send("_flashpaperloader","unload");
sendFlashConn = null;
}
try
{
if(recieveFlashConn != null)
{
recieveFlashConn.close();
recieveFlashConn = null;
}
}catch(e:ArgumentError)
{
trace(e.toString());
recieveFlashConn = null;
}
unloadAndStop(true);
//System.gc();
}
public function errUnload():void
{
if(sendFlashConn != null)
{
sendFlashConn.send("_flashpaperloader","unload");
sendFlashConn = null;
}
if(recieveFlashConn != null) recieveFlashConn = null;
unloadAndStop(true);
//System.gc();
}
} // end class
} // end package
{
import flash.events.Event;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
//import flash.system.System;
import mx.controls.SWFLoader;
/**
* UIComponent designed specifically to load FlashPaper documents
* and size them correctly in the available area.
*/
public class FlashPaperLoader extends SWFLoader
{
/**
* The id of the FlashPaperLoader.swf proxy that will be used
* for communication pyurposes.
*/
public static const FLASH_PAPER_LOADED : String = "flashPaperLoaded";
public static const FLASH_CONNERROR : String = "flashConnError";
private var sendFlashConn:LocalConnection;
private var recieveFlashConn:LocalConnection;
/**
* The name of the application that is loading in the FlashPaperLoader.swf
* file. This name must correspond to the "id" attribute in the object tag
* and the "name" attribute in the embed tag when the Flex client is embedded
* into the html page.
*/
/**
* Constructor
*/
public function FlashPaperLoader()
{
//source = "app-storage:/data/fpHolder.swf";
sendFlashConn=new LocalConnection();
recieveFlashConn = new LocalConnection();
recieveFlashConn.client=this;
sendFlashConn.addEventListener(StatusEvent.STATUS, onStatus);
sendFlashConn.allowDomain("*");
recieveFlashConn.allowDomain("*");
sendConn();
}
public function sendConn():void
{
try
{
recieveFlashConn.connect("_flexloader");
} catch (error:ArgumentError) {
trace("Can't connect...the connection name is already being used by another SWF");
onConnError();
return;
}
}
private function onStatus(result:StatusEvent) :void{
trace (result.level == "error"?"Operation failed":"Operation succeeded");
}
//连接源出错
private function onConnError():void
{
//errUnload();
var e:Event=new Event(FlashPaperLoader.FLASH_CONNERROR);
dispatchEvent( e );
}
// =================================================================
// Expose methods that are proxied from FlashPaperLoader.swf - Call
// JavaScript methods that the FlashPaperLoader.swf file picks up
// and passes to the loaded FlashPaper document.
// =================================================================
public function setSize( width:Number, height:Number ):void
{
trace("=========setPaperSize=============");
sendFlashConn.send("_flashpaperloader","setPaperSize",width,height);
}
/**
* 文档加载成功提示
* */
public function fpLoaded():void
{
trace("reveice fpLoaded message!! this.width = " + this.width + " this.height" + this.height);
//setSize(parent.width,parent.height);
var e:Event=new Event(FlashPaperLoader.FLASH_PAPER_LOADED);
dispatchEvent( e );
//this.visible=true;
}
/**
* 设置缩放
* */
public function setZoom(value:Object):void
{
if (this.visible)
{
sendFlashConn.send("_flashpaperloader","setCurrentZoom",value);
}
}
// override protected function updateDisplayList( unscaledWidth:Number,
// unscaledHeight:Number ):void
// {
// if ( contentHolder )
// {
// // Adjust values so the FlashPaper document is displayed correctly
// contentHolder.scaleX = 1.0;
// contentHolder.scaleY = 1.0;
// contentHolder.x = 0;
// contentHolder.y = 0;
//
// contentHolder.scrollRect = new Rectangle( 0, 0, unscaledWidth, unscaledHeight );
//
// // When the content has loaded, call the setSize method so that the
// // FlashPaper document sizes right in the available area
// if ( Loader( contentHolder ).content )
// {
// setSize( unscaledWidth, unscaledHeight );
// //this.setFocus();
// }
// }
// }
//卸载此swf
public function unload():void
{
if(sendFlashConn != null)
{
sendFlashConn.send("_flashpaperloader","unload");
sendFlashConn = null;
}
try
{
if(recieveFlashConn != null)
{
recieveFlashConn.close();
recieveFlashConn = null;
}
}catch(e:ArgumentError)
{
trace(e.toString());
recieveFlashConn = null;
}
unloadAndStop(true);
//System.gc();
}
public function errUnload():void
{
if(sendFlashConn != null)
{
sendFlashConn.send("_flashpaperloader","unload");
sendFlashConn = null;
}
if(recieveFlashConn != null) recieveFlashConn = null;
unloadAndStop(true);
//System.gc();
}
} // end class
} // end package