1.右键项目设置flex编译器的参数如下:
其实flash player从11.版本就支持自定义右键菜单了。
设置flash player 11.3后,项目编译会出错,提示在SDK安装目录下找不到playerglobal.swc,因为不同版本的flash player对应不同的playerglobal.swc,flash player 11.3对应的playerglobal.swc见附件。下载解压后,将playerglobal.swc放到SDK目录下frameworks\libs\player\11.3文件夹中。
3.测试代码如下:
<?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" applicationComplete="applicationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.controls.Menu; import mx.events.FlexEvent; import mx.events.MenuEvent; protected function applicationCompleteHandler(event:FlexEvent):void{ this.addEventListener(MouseEvent.RIGHT_CLICK,rightClickHandler); } private var myMenu:Menu = null ; protected function rightClickHandler(event:MouseEvent):void{ //定义了一个menu if(myMenu==null){ var menuData:Array = [ {label: "菜单1"},{label: "菜单2"},{label: "菜单3"} ]; myMenu = Menu.createMenu(this,menuData,false); myMenu.labelField = "label" ; myMenu.addEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler); myMenu.addEventListener(MouseEvent.ROLL_OUT,menuRollOutHandler); } //隐藏掉上一个右键菜单 myMenu.hide() ; myMenu.show(event.stageX,event.stageY); } protected function menuItemClickHandler(event:MenuEvent):void{ Alert.show(String(event.item.label),"点击"); } /** * 鼠标移出后隐藏右键菜单 */ protected function menuRollOutHandler(event:MouseEvent):void{ myMenu.hide() ; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Label text="测试"/> </s:Application>
效果如下: