Flex 右键 功能

工具类如下:

RightClickManager.as

 

package com.soa
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;

	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.Application;

	public class RightClickManager
	{
		static private var rightClickTarget:DisplayObject;
		static public const RIGHT_CLICK:String="rightClick";
		static private const javascript:XML=;

		public function RightClickManager()
		{
			return;
		}

		static public function regist():Boolean
		{
			if (ExternalInterface.available)
			{
				ExternalInterface.call(javascript, ExternalInterface.objectID);
				ExternalInterface.addCallback("rightClick", dispatchRightClickEvent);
				Application.application.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			} // end if  
			return true;
		}

		static private function mouseOverHandler(event:MouseEvent):void
		{
			rightClickTarget=DisplayObject(event.target);
			//trace(rightClickTarget.name);  
			//rightClickTarget = (rightClickTarget is IListItemRenderer)?rightClickTarget:null; // 如果不是在列表上,则取消右键事件  
			return;
		}

		static private function dispatchRightClickEvent():void
		{
			var event:MouseEvent;
			if (rightClickTarget != null)
			{
				//trace(rightClickTarget)  
				event=new MouseEvent(RIGHT_CLICK, true, false, rightClickTarget.mouseX, rightClickTarget.mouseY);
				rightClickTarget.dispatchEvent(event);
			} // end if  
			return;
		}

		//添加在不希望出现右键菜单比如弹出窗口上 .执行即可  
		static public function setRightClickTargetNULL():void
		{
			rightClickTarget=null;
		}


	}
}


RightClickRegister.as

 

package com.soa
{
	import flash.display.Sprite;

	import mx.utils.NameUtil;

	[Event(name="rightClick", type="flash.events.MouseEvent")]
	public dynamic class RightClickRegister extends Sprite
	{
		private var rightClickRegisted:Boolean=false;

		public function RightClickRegister()
		{
			if (!rightClickRegisted)
			{
				RightClickManager.regist();
				rightClickRegisted=true;
			}
			try
			{
				name=NameUtil.createUniqueName(this);
			}
			catch (e:Error)
			{
			}
			return;
		}

		public override function toString():String
		{
			return NameUtil.displayObjectToString(this);
		}
	}
}


调用例子如下:

rightClick.mxml



	
		
	
	
		
	
	
		
	

顺序说一下:

html-template/index.template.html 中需要增加

 

 



你可能感兴趣的:(Flex,flex)