FlashDemo.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="775" height="424" minWidth="955" minHeight="600" backgroundColor="#4D75B8"
creationComplete="onCreationComplete()"
applicationComplete="onApplicationComplete()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.utils.getQualifiedClassName;
static var mFlashServer: FlashServer = new FlashServer();
protected function btnTest_clickHandler(event:MouseEvent):void
{
mx.controls.Alert.show("我被点击了");
}
protected function btnGetInfo_clickHandler(event:MouseEvent):void
{
mFlashServer.getInfo();
}
private function onCreationComplete():void
{
//mx.controls.Alert.show("onCreationComplete");
}
private function onApplicationComplete():void
{
//Alert.show("onApplicationComplete");
//Alert.show(this.loaderInfo.toString());
var displayObj:* = this.loaderInfo.content.stage as flash.display.DisplayObjectContainer;
//var loadClassName:* = flash.utils.getQualifiedClassName(this.loaderInfo.content);
var loadClassName:* = flash.utils.getQualifiedClassName(displayObj);
//Alert.show(loadClassName);
//addEventListener(Event.ADDED_TO_STAGE,mFlashServer.AddedToStageHandler);
FlashUiCtrl.getInstance().mDisplayObj = displayObj;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button id="btnTest" x="106" y="39" width="201" height="95" label="点我"
click="btnTest_clickHandler(event)"/>
<s:Button id="btnGetInfo" x="130" y="165" width="201" height="74" label="getInfo"
click="btnGetInfo_clickHandler(event)"/>
<s:Button x="420" y="73" label="按钮"/>
<s:Button x="425" y="128" label="按钮"/>
<s:Button x="535" y="113" label="按钮"/>
</s:Application>
FlashUiCtrl.cs
package
{
public class FlashUiCtrl
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import mx.controls.Alert;
import mx.controls.Button;
import mx.utils.ObjectUtil;
private static var _instance: FlashUiCtrl = new FlashUiCtrl();
public var mDisplayObj:flash.display.DisplayObjectContainer = null;
public var mDtDisplayObj: Dictionary = new Dictionary(); //所有控件对象 key:name value:DisplayObject
public var mLoopNum: int = 0;
public function FlashUiCtrl()
{
super();
}
public static function getInstance():FlashUiCtrl
{
return _instance;
}
public function refreshCtrl():void
{
//Alert.show("start refreshCtrl");
if(this.mDisplayObj == null)
{
Alert.show("mDisplayObj == null");
return;
}
mLoopNum = 0;
mDtDisplayObj = new Dictionary();
this.traceDisplayList(this.mDisplayObj);
}
public function clickButton(btnName: String):void
{
doClickButton(btnName, mDisplayObj);
}
private function doClickButton(btnName: String, container:DisplayObjectContainer):void
{
if(container == null)
{
return;
}
var child:DisplayObject;
var numChildren:int = container.numChildren;
for (var i:uint=0; i < numChildren; i++)
{
if(i >= numChildren) // 调试发现,不加这句 程序会卡死
{
break;
}
child = container.getChildAt(i);
var loadClassName:String = flash.utils.getQualifiedClassName(child);
if(loadClassName == "FlashDemo")
{
//(child as FlashDemo).btnTest.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
// var objInfo:Object = ObjectUtil.getClassInfo(child);
// var fieldName:Array = objInfo["properties"] as Array;
// for each(var q:QName in fieldName){
// var value:String = (child as FlashDemo)[q.localName] as String;
// }
((child as FlashDemo)[btnName]).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
return;
}
if (child is DisplayObjectContainer)
{
doClickButton(btnName, DisplayObjectContainer(child));
}
}
}
public function traceDisplayList(container:DisplayObjectContainer):void
{
//Alert.show("start traceDisplayList");
if(container == null)
{
return;
}
//Alert.show("container.numChildren=" + container.numChildren);
var child:DisplayObject;
var numChildren:int = container.numChildren;
for (var i:uint=0; i < numChildren; i++)
{
if(i >= numChildren) break; // 不加这句 程序会卡死
child = container.getChildAt(i);
var loadClassName:String = flash.utils.getQualifiedClassName(child);
trace("child.name=", child.name, "loadClassName=", loadClassName);
if(loadClassName == "FlashDemo")
{
//(child as FlashDemo).getChildByName("btnTest").dispatchEvent(new MouseEvent(MouseEvent.CLICK));
trace((child as FlashDemo).toString());
trace((child as FlashDemo).parent.numChildren);
var claInfo:Object = ObjectUtil.getClassInfo(child);
var props:Array = claInfo["properties"];
for each(var q:QName in props){
//trace(q.localName);
}
(child as FlashDemo).btnTest.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
return;
}
if (child is DisplayObjectContainer)
{
traceDisplayList(DisplayObjectContainer(child))
}
else
{
//Alert.show(child.name);
mDtDisplayObj[child.name] = child;
}
}
} // end function traceDisplayList
}
}
FlashServer.cs
package
{
import flash.display.Sprite;
import mx.managers.SystemManager;
public class FlashServer extends flash.display.Sprite
{
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import mx.controls.Alert;
import mx.managers.SystemManager;
import flash.display.DisplayObject;
import flash.utils.getQualifiedClassName;
import flash.events.Event;
import flash.system.Security;
public function FlashServer()
{
super();
ExternalInterface.addCallback("getInfo", this.getInfo);
ExternalInterface.addCallback("clickButton", this.clickButton);
//addEventListener(Event.ADDED_TO_STAGE,AddedToStageHandler);
flash.system.Security.allowDomain("*");
flash.system.Security.allowInsecureDomain("*");
}
public function AddedToStageHandler(e:Event):void
{
Alert.show("AddedToStageHandler");
}
// 点击按钮
public function clickButton(btnName:String):String
{
//Alert.show("进入 clickButton btnName=" + btnName);
try
{
var sysMgr: SystemManager = SystemManager(SystemManager.getSWFRoot(this));
FlashUiCtrl.getInstance().mDisplayObj = sysMgr.application.loaderInfo.content.stage as flash.display.DisplayObjectContainer;
FlashUiCtrl.getInstance().clickButton(btnName);
// var dispObj:DisplayObject = FlashUiCtrl.getInstance().mDtDisplayObj[btnName];
// if(dispObj != null)
// {
// dispObj.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
// }
}
catch(e: Error)
{
trace(e);
return "failed";
}
return "success";
}
//
public function getInfo():String
{
return "KO2020";
}
}
}
miniblink
void MB_CALL_TYPE CMiniblinkDemoDlg::onMbRunJsCallback(mbWebView webView, void* param, mbJsExecState es, mbJsValue v)
{
CMiniblinkDemoDlg* pCDlgMain = (CMiniblinkDemoDlg*)param;
if (pCDlgMain == NULL) return;
std::string result = mbJsToString(es, v);
MessageBoxA(NULL, result.c_str(), "", MB_OK);
}
void CMiniblinkDemoDlg::OnBnClickedButton1()
{
std::string js;
js = "eval(FlashDemo.clickButton('btnTest'));";
js = "FlashDemo.clickButton('btnTest');";
::mbRunJs(mWebView, mbWebFrameGetMainFrame(mWebView), js.c_str(), FALSE, onMbRunJsCallback, this, NULL);
js = "";
}
void CMiniblinkDemoDlg::OnBnClickedBtnGetInfo()
{
std::string js;
js = "FlashDemo.getInfo();";
::mbRunJs(mWebView, mbWebFrameGetMainFrame(mWebView), js.c_str(), FALSE, onMbRunJsCallback, this, NULL);
}
效果: