Flex 右键 功能
程序员文章站
2022-05-05 19:54:32
...
工具类如下:
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=<script> <![CDATA[ function(flashObjectId) { var RightClick = { init: function (flashObjectId) { this.FlashObjectID = flashObjectId; this.Cache = this.FlashObjectID; if(window.addEventListener){ window.addEventListener("mousedown", this.onGeckoMouse(), true); } else { document.getElementById(this.FlashObjectID).parentNode.onmouseup = function() { document.getElementById(RightClick.FlashObjectID).parentNode.releaseCapture(); } document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }} document.getElementById(this.FlashObjectID).parentNode.onmousedown = RightClick.onIEMouse; } }, killEvents: function(eventObject) { if(eventObject) { if (eventObject.stopPropagation) eventObject.stopPropagation(); if (eventObject.preventDefault) eventObject.preventDefault(); if (eventObject.preventCapture) eventObject.preventCapture(); if (eventObject.preventBubble) eventObject.preventBubble(); } }, onGeckoMouse: function(ev) { return function(ev) { if (ev.button != 0) { RightClick.killEvents(ev); if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) { RightClick.call(); } RightClick.Cache = ev.target.id; } } }, onIEMouse: function() { if (event.button ==2||event.button==0) { if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) { RightClick.call(); } document.getElementById(RightClick.FlashObjectID).parentNode.setCapture(); if(window.event.srcElement.id) RightClick.Cache = window.event.srcElement.id; } }, call: function() { document.getElementById(this.FlashObjectID).rightClick(); } } RightClick.init(flashObjectId); } ]]> </script>; 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
<?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" minWidth="955" minHeight="600" creationComplete="init()"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import com.soa.RightClickManager; import com.soa.RightClickRegister; import mx.controls.Alert; protected var rightClickRegisted:Boolean = false; private function init():void { if (!rightClickRegisted) { RightClickManager.regist(); rightClickRegisted = true; } lae.addEventListener(RightClickManager.RIGHT_CLICK,treeRightClickHandler); } private function treeRightClickHandler(eve:MouseEvent):void { Alert.show((eve.target as Label).text ); } private function c():void{Alert.show("2222");} ]]> </fx:Script> <s:Label id="lae" x="284" y="246" width="141" height="123" fontSize="36" text="1111" click="c()"> </s:Label> </s:Application>
顺序说一下:
html-template/index.template.html 中需要增加
<param name="wmode" value="opaque" />