本文引用:http://hi.baidu.com/178link/blog/item/3e0471a843556eb5cb130c97.html
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="init()" >
<mx:Script>
<![CDATA[
import flash.display.Sprite;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.events.ContextMenuEvent;
//在全局上,定义三个栏目
private var menu1:ContextMenuItem;
private var menu2:ContextMenuItem;
private var menu3:ContextMenuItem;
//初始化开始了
public function init():void{
textSelect.text = "RightClick is Default";
menu1= new ContextMenuItem("Draw Red");
menu2 = new ContextMenuItem("Draw Yellow");
menu3 = new ContextMenuItem("Default");
//让这三个栏目监听点事件
menu1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
menu2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
menu3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menudraw);
//这个就是把那些事件全部加到一起,放在ContextMenu上
var MyMenu:ContextMenu = new ContextMenu();
MyMenu.hideBuiltInItems();
MyMenu.customItems.push(menu1);
MyMenu.customItems.push(menu2);
MyMenu.customItems.push(menu3);
//这个是定义那个部位来监听,如果你定义了一个实例,想要你的实例监听你的右键,那么把this 改为你的实例名字
//这个是整个舞台的监听.
this.contextMenu = MyMenu;
}
//选择选择到了右键栏目,改干点啥
private function menudraw(e:ContextMenuEvent):void {
switch (e.target) {
case menu1 :
textSelect.text = "RightClick IS READ";
break;
case menu2 :
textSelect.text = "RightClick IS Yellow";
break;
case menu3 :
textSelect.text = "RightClick is Default";
break;
}
}
]]>
</mx:Script>
<mx:Label x="97" y="60" id="textSelect" text="Test Right-Click"/>
</mx:Application>