flex中定制右键菜单


一、将flex的所有右键屏蔽,并响应右键
第一步:
在swf 所在html页加如下内容:
<script>
function onNsRightClick(e){
if(e.which == 3){
  ownerarea.openRightClick();
  e.stopPropagation();
}
return false;
}

function onIeRightClick(e){
if(event.button > 1){
  ownerarea.openRightClick();
  parent.frames.location.replace('javascript: parent.falseframe');
}
return false;
}


if(navigator.appName == "Netscape"){
document.captureEvents(Event.MOUSEDOWN);
document.addEventListener("mousedown", onNsRightClick, true);
}
else{
document.onmousedown=onIeRightClick;
}

</script>

第二步:
swf所在html文件中AC_FL_RunContent中增加
"wmode", "opaque",

第三步:flex中
<mx:Application creationComplete="init();" mouseOver="getMouseTarget(event)">
<mx:Script>
private var mouseTarget:DisplayObject;

private function init(): void{
ExternalInterface.addCallback("openRightClick", openRightClick);
}

function openRightClick():void
{
var e:MouseEvent = new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, mouseTarget.mouseX, mouseTarget.mouseY);
mouseTarget.dispatchEvent(e);
}

function getMouseTarget(event:MouseEvent):void
{
   mouseTarget = DisplayObject(event.target);
}

function showMouseEvent(event){
//通过event.buttonDown为true和flash可以得到左键点击或右键点击
// 但加入菜单没有效果,需要再研究
var menu: Menu = new Menu();
menu = Menu.createMenu(null, buildMenu);
menu.labelField = "@label";
menu.show(10,10);
}
</mx:Script>
<mx:XML format="e4x" id="buildMenu">
<root>
<menuitem label="新建">
</menuitem>
</root>
</mx:XML>
<mx:Image x="62" source="../images/info_btn.gif" id="baseDataBtn" bottom="0" mouseDown="showMouseEvent(event)"/>
</mx:Application>

你可能感兴趣的:(JavaScript,html,Flex,Flash)