第一步:到http://rightclickmanager.googlecode.com/下载rightclickmanager-source-lib-0.1.rar并引入自己的FLEX工程,这是实现右键点击事件,及完全屏闭系统右键菜单的关键!
第二步:打开: <主程序名>.template.html 文件,修改为:
<!-- AC_FL_runcontent( "src","${swf}","id","${application}","name","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; } } }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;
}
第五步:
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,rightClickTarget as InteractiveObject); rightClickTarget.dispatchEvent(event); }// end if return; }
到这里,功能就完成了,本人修改了RightClickManager的事件从MouseEvent改为ContextMenuEvent是为了能在列表控件DataGrid/Tree/List上点右键时自动选择当前行,使用了ContextMenuEvent事件中的event.mouseTarget和列表控件的IListItemRenderer接口!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。