[原文http://blog.csdn.net/cjy37/article/details/3954516]
第一步:到http://rightclickmanager.googlecode.com/下载rightclickmanager-source-lib-0.1.rar并引入自己的FLEX工程,这是实现右键点击事件,及完全屏闭系统右键菜单的关键!
第二步:打开: <主程序名>.template.html 文件,修改为:
<!-- AC_FL_RunContent( "src", "${swf}", "id", "${application}", "name", "${application}", "width", "${width}", "height", "${height}", "align", "middle", "menu", "false", "wmode", "opaque",//注意:这里是右键屏蔽的关键 "quality", "high", "bgcolor", "${bgcolor}", "allowScriptAccess", "always", "allowNetworking", "all", "allowFullScreen", "true",//是否可全屏的参数 "type", "application/x-shockwave-flash", "pluginspage", "http://www.adobe.com/go/getflashplayer" ); //-->
第三步:在主程序文件.as中引入:
import com.siloon.plugin.rightClick.RightClickManager;
并加如下代码:
[Event(name="rightClick",type="fla sh.events.ContextMenuEvent")] public class index { protected var rightClickRegisted:Boolean = false; public function index() { super(); if (!rightClickRegisted) { RightClickManager.regist(); rightClickRegisted = true; } try { name = NameUtil.createUniqueName(this); } catch (e:Error) { } return; } public override function toString() : String { return NameUtil.displayObjectToString(this); }
第四步:在要弹出右键的控件的初始化代码中加入:
NavTabTree.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);
再加以下函数来处理:
/** * 在控制上单击右键菜单事件的处理函数 * @param event 右键事件 * */ protected function rightClickHandler(event:ContextMenuEvent):void { onRightClicked(event); removeMenu(); menu = new Menu(); menu = Menu.createMenu(this, createMenuItems(), false); menu.labelField="label" menu.variableRowHeight = true; //menu.addEventListener(MenuEvent.ITEM_CLICK, menuItemClickHandler); var point:Point = new Point(mouseX,mouseY); point = localToGlobal(point); menu.show(point.x,point.y); } /** * 如果显示过Menu,则先释放资源 * */ private function removeMenu():void { if(menu!=null) { menu.hide(); //menu.removeEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler); menu=null; } } /** * 创建右键菜单选项 * @return 返回选项组成的Array * */ private function createMenuItems():Array { var menuItems:Array = new Array(); var menuItem:Object; menuItem = new Object; menuItem.label = '清空画布'; menuItem.action = 'clearAll'; menuItems.push(menuItem); menuItem = new Object; menuItem.label = '选择背景'; menuItem.action = 'selectBackImage'; menuItems.push(menuItem); return menuItems; } /** * 当右键单击Tree的Item时,自动选择鼠标所在的当前项 * @param e 自定义右键菜单事件 * */ private function onRightClicked(e:ContextMenuEvent):void { var rightClickItemRender:IListItemRenderer; var rightClickIndex:int; if(e.mouseTarget is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget); }else if(e.mouseTarget.parent is IListItemRenderer) { rightClickItemRender = IListItemRenderer(e.mouseTarget.parent); } if(rightClickItemRender != null) { rightClickIndex = NavTabTree.itemRendererToIndex(rightClickItemRender); if(NavTabTree.selectedIndex != rightClickIndex) { NavTabTree.selectedIndex = rightClickIndex; } } }
第五步:
修改RightClickManager.as文件的
static private var rightClickTarget:*;
再修改两个该文件的两个函数:
static private function mouseOverHandler(event:MouseEvent) : void { rightClickTarget = InteractiveObject(event.target); return; } static private function dispatchRightClickEvent() : void { var event:ContextMenuEvent; if (rightClickTarget != null) { event = new ContextMenuEvent(RIGHT_CLICK, true, false, rightClickTarget as InteractiveObject, rightClickTarget as InteractiveObject); rightClickTarget.dispatchEvent(event); }// end if return; }
到这里,功能就完成了,本人修改了RightClickManager的事件从MouseEvent改为ContextMenuEvent是为了能在列表控件DataGrid/Tree/List上点右键时自动选择当前行,使用了ContextMenuEvent事件中的event.mouseTarget和列表控件的IListItemRenderer接口!
本功能也有副作用,系统右键菜单失效,那文本的右键复制粘贴也就失效了,有大量用到TEXTINPUT的就要小心使用了~!