直接上代码吧,关键地方都加上注释了:
01
var
cm:ContextMenu=
new
ContextMenu();
02
cm.hideBuiltInItems();
//隐藏系统内置的菜单
03
04
//创建菜单项(构造函数参数含义:标题名字,前面是否增加分割线,是否可用,是否可见)
05
var
menuItems:
Array
= [
06
new
ContextMenuItem(
"菜单1"
,
false
,
true
,
true
),
07
new
ContextMenuItem(
"菜单2"
,
false
,
true
,
true
),
08
new
ContextMenuItem(
"菜单3"
,
false
,
false
,
true
),
09
new
ContextMenuItem(
"菜单4"
,
true
,
true
,
true
),
10
new
ContextMenuItem(
"菩提树下的杨过"
,
false
,
false
,
false
)
11
];
12
13
cm.customItems = menuItems;
14
15
//注册菜单项点击事件
16
for
(
var
i:
uint
=
0
;i<menuItems.length;i++){
17
menuItems[i].addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,MenuItemSelectHandler);
18
}
19
20
function
MenuItemSelectHandler(e:ContextMenuEvent) {
21
txtMsg.text =
"您点击了:"
+ e.currentTarget.caption;
//注:txtMsg是舞台上的一个动态文本
22
}
23
24
this
.contextMenu=cm;
25
26
txtMsg.text =
"请右击测试右键菜单"
;