遍历flex控件并自动点击按钮 遍历flash控件并自动点击按钮

FlashDemo.mxml



	
	
		
	
	
	
		
	
	
	
	
	
	

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);
}

效果:

遍历flex控件并自动点击按钮 遍历flash控件并自动点击按钮_第1张图片

 

 

 

 

你可能感兴趣的:(游戏辅助,Flex)