[转]as3 鼠标右键菜单类 CustomerContextMenu

[url]http://hi.baidu.com/xinhua0228/blog/item/094e363b3bb147e215cecb86.html[/url]


package {
import flash.events.ContextMenuEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
/**
* ...
* @author liuxh
*/
public class CustomerContextMenu
{
private var _contextMenu:ContextMenu;

/**
* @param removeAll 是否禁用所有默认菜单
* @param listener 自定义菜单事件句柄
* @param ...customItems 自定义标签,可以是一个数组
*/
public function CustomerContextMenu(removeAll:Boolean = true, listener:Function = null, ...customItems)
{
_contextMenu = new ContextMenu();
if(removeAll)
{
removeDefault();
}
if(customItems.length > 0 && listener != null)
{
addCustomItems(listener, customItems);
}
}
//##########################################################################
//添加属性
//##########################################################################
public function get contextMenu():ContextMenu
{
return _contextMenu;
}

public function get builtInItems():ContextMenuBuiltInItems
{
return _contextMenu.builtInItems;
}
//##########################################################################
//
// Methods 方法
//
//##########################################################################
/**
* 禁用默认菜单
* @param ...leave 保留默认菜单(标签数组)
*/
public function removeDefault(...leave):void
{
_contextMenu.hideBuiltInItems();
if(leave.length == 0)
{
return;
}
var defaultItems:ContextMenuBuiltInItems = _contextMenu.builtInItems;
for each(var item:String in leave)
{
defaultItems[item] = true;
}
}
/**
* 添加自定义菜单项
* @param caption 菜单项标题
* @param listener 事件句柄
* @param separatorBefore 是否在菜单上方添加分割线
* @param enabled 是否可用
* @param visible 是否可见
*/
public function addCustom(listener:Function, caption:String, separatorBefore:Boolean = false, enabled:Boolean = true,
visible:Boolean = true):void
{
var item:ContextMenuItem = new ContextMenuItem(caption, separatorBefore, enabled, visible);
_contextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, listener);
}
/**
* 添加自定义菜单组,所有菜单项共用同一个事件句柄
* 通过 event.currentTarget.caption 区分不同的菜单项
* private function onSelect(event:ContextMenuEvent):void{
* var item:ContextMenuItem = event.currentTarget;
* switch(item.caption)...}
* @param listener 事件句柄
* @param separatorBefore 是否在菜单组上方添加分隔线
* @param ...customItems 菜单项标题,可以是一个数组
*/
public function addGroup(listener:Function, separatorBefore:Boolean = false, ...customItems):void
{
addCustomItems(listener, customItems, separatorBefore);
}
//##########################################################################
//添加自定义菜单项
//##########################################################################
private function addCustomItems(listener:Function, customItems:Array, separatorBefore:Boolean = false):void
{
var itemArr:Array = customItems.length == 1 && customItems[0] is Array ? customItems[0] : customItems;
for each(var caption:String in itemArr)
{

addCustom(listener, caption, separatorBefore);
if(separatorBefore)
{
separatorBefore = false;
}
}
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值