也可以follow catfly围脖:t.sina.com.cn/iscat
在flash中每一个可以与鼠标和键盘交互的显示对象都有这么个属性:contextMenu
查看文档可以看到,从InteractiveObject这个抽象基类开始有这个属性。
所以DisplayObjectContainer, SimpleButton, TextField 这些显示对象都可以你都可以设置它们的 contextMenu属性,来隐藏你不需要的默认菜单项,和添加自定义的菜单项。
var my_menu:ContextMenu = new ContextMenu();
my_menu.hideBuiltInItems();
DisplayObjectContainer.contextMenu = my_menu;
这是最简单隐藏所有默认菜单项的方法(“设置” 与 “关于”两项除外 )。
如果你不想一次隐藏所有项,也可以一项项设置哪些项隐藏。
var my_menu:ContextMenu = new ContextMenu();
my_menu.builtInItems.forwardAndBack = false;
my_menu.builtInItems.loop = false;
my_menu.builtInItems.play = false;
my_menu.builtInItems.print = false;
my_menu.builtInItems.quality = false;
my_menu.builtInItems.rewind = false;
my_menu.builtInItems.save = false;
my_menu.builtInItems.zoom = false;
DisplayObjectContainer.contextMenu = my_menu;
那么可不可以完全隐藏所有菜单项呢?目前我还没有找到这样的方法,只看到这样一段话:
If you test your movie now you should see that your default zoom and playback settings are gone from your context menu. You should note that certain items such as Settings and About cannot be removed from the context menu. Some additional commands for debugging cannot also be removed either while testing in the Flash authoring tool.
添加自己的菜单项,就用下面的方法:
var my_menu:ContextMenu = new ContextMenu();
my_menu.hideBuiltInItems();
var my_blog:ContextMenuItem= new ContextMenuItem("猫脸爬格子");
var my_email:ContextMenuItem = new ContextMenuItem("rman@live.cn");
var my_copyright:ContextMenuItem = new ContextMenuItem("Copyright - 2009");
my_menu.customItems.push(my_blog,my_email,my_copyright);
DisplayObjectContainer.contextMenu = my_menu;
每一个ContextMenuItem对象除了菜单文字外,还有两个属性比较有用。
enabled : Boolean 指示指定的菜单项处于启用状态还是禁用状态(能否点击)。
separatorBefore : Boolean 指示指定的菜单项上方是否显示分隔条。
my_copyright.enabled = false;
my_copyright.separatorBefore = true;
为ContextMenuItem对象添加menuItemSelect 事件:ContextMenuEvent.MENU_ITEM_SELECT
var url:String;
my_blog.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);
my_email.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);
function openLink(e:ContextMenuEvent):void{
switch(e.currentTarget.caption){
case 猫脸爬格子:
url = "www.xncat.com";
break;
case rman@live.cn:
url = "www.xncat.com/email";
break;
}
navigateToURL(new URLRequest(url));
}
有网友写了个ContextMenuPlus类,可以很方便的处理以上各菜单定制功能。






