Flex屏蔽默认右键菜单。


  首先需要修改index.template.html生成的静态模板页面,使用js监听document.onmousedown事件,并调用Flex里暴露的方法,代码如下:(Index为生成的flash对象id) function onNsRightClick(e){
  if(e.which ==3){
  Index.openRightClick();
  e.stopPropagation(); 
  }
  returnfalse;
  }
  function onIeRightClick(e){
  if(event.button >1){
  Index.openRightClick();
  parent.frames.location.replace('javascript: parent.falseframe');
  }
  returnfalse;
  }
  if(navigator.appName =="Netscape"){
  document.captureEvents(Event.MOUSEDOWN); 
  document.addEventListener("mousedown", onNsRightClick, true); 
  }
  else{
  document.onmousedown=onIeRightClick; 
  }
  第二步,还需要修改index.template.html默认生成flash的脚本,在AC_FL_RunContent方法调用里添加一行"wmode", "opaque",参数设置,这是关键。
  最后,在Flex端注册暴露能被js调用的函数,ExternalInterface.addCallback("openRightClick", openRightClick);我的是写在Application的creationComplete事件里的,另外增加Application的mouseOver、mouseDown事件,在showMouseEvent方法里就能自定义模拟显示右键菜单了,mxml代码如下: 
  
  
  Flex/Login.aspx" method="get" showBusyCursor="true" result="httpsLogin_Result()">
  
  
  {this.txtUserName.text}
  
  
  {this.txtPassword.text}
  
  
  
  
  = 18 ? "no" : "yes");
  ExternalInterface.addCallback("openRightClick", openRightClick);
  }
  private function getMouseTarget(event:MouseEvent):void
  {
  mouseTarget = DisplayObject(event.target);
  }
  private function openRightClick():void
  {
  var e:MouseEvent = new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, mouseTarget.mouseX, mouseTarget.mouseY);
  mouseTarget.dispatchEvent(e);
  }
  private function showMouseEvent(event:MouseEvent):void
  {
  if(event.buttonDown == true)
  Alert.show("Left");
  else
  Alert.show("Right");
  }
  ]]>
  
  width="700" height="255" source="/style/blog/mobile.swf?menu=true&currentStat e=sunny&tempVar=11">
  
  width="960" height="58" horizontalGap="50" paddingTop="25">
  
  
  
  
  
  width="960" visible="false">
  width="364" height="234" backgroundAlpha="2" layout="absolute" fontSize="12" title="个人档案">
  
  
  
  
  
  
  width="364" height="234" layout="absolute" fontSize="12" title="日历">
  
  
  
  
  
  凡事有利就有弊,由于屏蔽了右键菜单,TextInput文本框默认也没有粘贴的选项了,由于设置了flash的wmode为opaque,导致一些中文输入的反作用,所以没有特别的需求还是不屏蔽到默认右键菜单,如果要加上一些自己定义的上下文菜单,Flex内也很简单,在Application的上下文内添加条不可选的菜单项实现如下: // 隐藏一些内建的鼠标右键菜单项
  this.contextMenu.hideBuiltInItems(); 
  var contextMenuItem : ContextMenuItem =new ContextMenuItem("Powered By: Jonllen");
  contextMenuItem.enabled =false;
  contextMenu.customItems.push(contextMenuItem);
  this.contextMenu.customItems.push(contextMenuItem);

你可能感兴趣的:(Flex)