Flex 右键菜单

MXML:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="20" initialize="doinit()"   mouseOver="getMouseTarget(event)">
 <mx:ArrayCollection id="dgArray">
  <mx:Object pid="1" name="秦始皇" time="秦" />
  <mx:Object pid="2" name="汉高祖" time="汉" />
  <mx:Object pid="3" name="唐太宗" time="唐" />
 </mx:ArrayCollection>
 <mx:Style>
  .myTitle{
   fontSize: 12;
   fontWeight: bolt;
   fontFamily: 黑体;
  }
 </mx:Style>
    <mx:Script>


        <![CDATA[
         import mx.events.MenuEvent;
         import mx.events.FlexMouseEvent;
         import mx.controls.Menu;
         import mx.collections.ArrayCollection;
            import mx.controls.Alert;
     import events.EventDispatcherExample;
            [Bindable]
            private var dp:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
   [Bindable]
   public var mouseTarget:DisplayObject;
            private function clickDouble(evt:MouseEvent):void{
             if(evt.target == null){
              return;
             }
             if(!evt.target.hasOwnProperty("data")){
              return;
             }
             var obj:Object = evt.target.data;
             Alert.show(obj.name + "是" + obj.time + "朝人!",obj.pid,4,this,null,null,null);
             trace(obj);
            }

   private function doinit():void{
    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 showRightMenu(evt:MouseEvent):void{
    if(evt.buttonDown == false){
     var obj:Object = evt.currentTarget;
     if(obj is DataGrid){
      var menu:Menu = this.createMenu(this,evt);
      menu.addEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler);
      evt.stopImmediatePropagation();
     }
    }
            }
            private function createMenu(menuParent:DisplayObjectContainer,evt:MouseEvent):Menu{
    var itemsArray:Array = this.createMenuItems(evt);
             if(itemsArray == null || itemsArray.length == 0) return null;
             var menu:Menu = new Menu();
             menu = Menu.createMenu(menuParent,itemsArray,false);
             menu.labelField = "label";
             menu.width = 160;
             menu.variableRowHeight = true;
             
             var point:Point = new Point(menuParent.mouseX,menuParent.mouseY);
             point = menuParent.localToGlobal(point);
    var yy:Number
    if (point.y+((420/19)*itemsArray.length)>Application.application.height){
     yy=point.y-(420/19)*itemsArray.length
    }else
     yy=point.y
    menu.show(point.x,yy);
             return menu;
            }
            private function createMenuItems(evt:Event):Array{
    if(!evt.target.hasOwnProperty("data") )return new Array();
    var menuItems:Array = new Array();
    menuItems = addToMenuArray(menuItems,"什么事","what")
    menuItems = addToMenuArray(menuItems,"什么时候","when")
    menuItems = addToMenuArray(menuItems,"什么原因","why")
    return menuItems;
            }
            private static function addToMenuArray(menuArray:Array,lbl:String,action:String):Array{
             if(menuArray == null) {
              menuArray = new Array();
             }
             var menuItem:Object = new Object();
             menuItem.label = lbl;
             menuItem.action = action;
             menuArray.push(menuItem);
             return menuArray;
            }
            
   private  function menuItemClickHandler(evt:MenuEvent):void{
    var menuItem:Object=evt.item
    var inx:int
    var item:Object
    if(menuItem.action == "what"){ //系统个性化配置
     Alert.show("什么事?","what");
    }
    if(menuItem.action == "when"){ //系统个性化配置
     Alert.show("什么时候?","when");
    }
    if(menuItem.action == "why"){ //系统个性化配置
     Alert.show("什么原因?","why");
    }
   } 
        ]]>
    </mx:Script>

    <mx:Panel title="Repeater Example" width="75%" height="75%"
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
 
        <mx:Text width="100%" color="blue"
            text="Use the Repeater class to create 9 Button controls in a 3 by 3 Tile container."/>

        <mx:Tile direction="horizontal" borderStyle="inset"
            horizontalGap="10" verticalGap="15"
            paddingLeft="10" paddingTop="10" paddingBottom="10" paddingRight="10">
            <mx:Repeater id="rp" dataProvider="{dp}">
             <mx:CheckBox label="{rp.currentItem}" />
            </mx:Repeater>
        </mx:Tile>
    <mx:DataGrid id="dg" doubleClickEnabled="true" doubleClick="clickDouble(event)" dataProvider="{dgArray}"
     mouseDown="showRightMenu(event)" mouseOver="getMouseTarget(event)"  >
     <mx:columns>
      <mx:DataGridColumn headerText="序号" dataField="id"/>
      <mx:DataGridColumn headerText="名字" dataField="name"/>
      <mx:DataGridColumn headerText="朝代" dataField="time"/>
     </mx:columns>
    </mx:DataGrid>
    </mx:Panel> 
</mx:Application>

模板HTML:

1。
 AC_FL_RunContent(

   "menu", "false",  
  "wmode", "opaque",
}  

2。
function onNsRightClick(e){
  if(e.which == 3){
   ${application}.openRightClick();
   e.stopPropagation();
  }
  return false;
 }

 function onIeRightClick(e){
  if(event.button > 1){
   ${application}.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.οnmοusedοwn=onIeRightClick;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值