Flex自定义右键菜单

  1. 自定义右键菜单注册类:
    项目中新增注册类 RightClickManager,代码如下:
    package com.siloon.plugin.rightClick
    {
         import flash.display.DisplayObject;
         import flash.display.InteractiveObject;
         import flash.events.ContextMenuEvent;
         import flash.events.MouseEvent;
         import flash.external.ExternalInterface;
        
         import mx.core.Application;
        
         public class RightClickManager
         {
              static private var rightClickTarget:DisplayObject;
            static public const RIGHT_CLICK:String = "rightClick";
            static private const javascript:XML =
            <script>
                 <![CDATA[
                      /**
                        *
                        * Copyright 2007
                        *
                        * Paulius Uza
                        * http://www.uza.lt
                        *
                        * Dan Florio
                        * http://www.polygeek.com
                        *
                        * Project website:
                        * http://code.google.com/p/custom-context-menu/
                        *
                        * --
                        * RightClick for Flash Player.
                        * Version 0.6.2
                        *
                        */
                        function(flashObjectId)
                        {                   
                             var RightClick = {
                                  /**
                                  *  Constructor
                                  */
                                  init: function (flashObjectId) {
                                       this.FlashObjectID = flashObjectId;
                                       this.Cache = this.FlashObjectID;
                                       if(window.addEventListener){
                                            window.addEventListener("mousedown", this.onGeckoMouse(), true);
                                       } else {
                                            document.getElementById(this.FlashObjectID).parentNode.onmouseup = function() { document.getElementById(RightClick.FlashObjectID).parentNode.releaseCapture(); }
                                            document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
                                            document.getElementById(this.FlashObjectID).parentNode.onmousedown = RightClick.onIEMouse;
                                       }
                                  },
                                  /**
                                  * GECKO / WEBKIT event overkill
                                  * @param {Object} eventObject
                                  */
                                  killEvents: function(eventObject) {
                                       if(eventObject) {
                                            if (eventObject.stopPropagation) eventObject.stopPropagation();
                                            if (eventObject.preventDefault) eventObject.preventDefault();
                                            if (eventObject.preventCapture) eventObject.preventCapture();
                                               if (eventObject.preventBubble) eventObject.preventBubble();
                                       }
                                  },
                                  /**
                                  * GECKO / WEBKIT call right click
                                  * @param {Object} ev
                                  */
                                  onGeckoMouse: function(ev) {
                                         return function(ev) {
                                      if (ev.button != 0) {
                                            RightClick.killEvents(ev);
                                            if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
                                                RightClick.call();
                                            }
                                            RightClick.Cache = ev.target.id;
                                       }
                                    }
                                  },
                                  /**
                                  * IE call right click
                                  * @param {Object} ev
                                  */
                                  onIEMouse: function() {
                                         if (event.button > 1) {
                                            if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
                                                 RightClick.call();
                                            }
                                            document.getElementById(RightClick.FlashObjectID).parentNode.setCapture();
                                            if(window.event.srcElement.id)
                                            RightClick.Cache = window.event.srcElement.id;
                                       }
                                  },
                                  /**
                                  * Main call to Flash External Interface
                                  */
                                  call: function() {
                                       document.getElementById(this.FlashObjectID).rightClick();
                                  }
                             }
                            
                             RightClick.init(flashObjectId);
                        }
                 ]]>
            </script>;

            public function RightClickManager()
            {
                return;
            }

            static public function regist() : Boolean
            {
                if (ExternalInterface.available)
                {
                    ExternalInterface.call(javascript, ExternalInterface.objectID);
                    ExternalInterface.addCallback("rightClick", dispatchRightClickEvent);
                    Application.application.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
                }// end if
                return true;
            }

            static private function mouseOverHandler(event:MouseEvent) : void
            {
                   //rightClickTarget = DisplayObject(event.target);
                   rightClickTarget = InteractiveObject(event.target);
                return;
            }

            static private function dispatchRightClickEvent() : void
            {
                   var event:MouseEvent;
                if (rightClickTarget != null)
                {
                    event = new MouseEvent(RIGHT_CLICK, true, false, rightClickTarget.mouseX, rightClickTarget.mouseY);
                        //event = new ContextMenuEvent(RIGHT_CLICK, true, false, rightClickTarget as InteractiveObject, rightClickTarget as InteractiveObject);
                        rightClickTarget.dispatchEvent(event);
                }// end if
                return;
            }

         }
    }


  2.  打开自己的Flex工程下的html-template文件夹下的index.template.html文件(右击-Open With-Text Editor),在var params = {};语句的下面添加下面的语句:
    params.wmode = "opaque";//屏蔽系统右键菜单的关键


    3.  在主程序文件中引入:
     
    //初始化
    protected function init():void
                   {
                        if (!rightClickRegisted) 
                        { 
                             maxNumText.text=rightClickRegisted.toString();
                             RightClickManager.regist(); 
                             rightClickRegisted = true; 
                        }
                        this.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);
                        maxNumText.text+="init";
                   }

    //创建菜单项
                   private function createMenuItems():Array 
                   { 
                       
                        var menuItems:Array = new Array();
                       
                       
                       
                        var menuItem:Object;
                       
                        menuItem = new Object; 
                       
                        menuItem.label = '刷新'; //菜单项名称
                       
                        //menuItem.itemIcon = this.menu_SX;//菜单项图标
                       
                        menuItems.push(menuItem);
                       
                        return menuItems; 
                       
                   }
                  
                   //生成右键菜单
                  
                   private function initMenu():void
                   {
                        menu = Menu.createMenu(this, createMenuItems(), false);
                       
                        //menu.iconField="itemIcon";//右键菜单的图标
                       
                        //menu.labelField="label";  //右键菜单的名称
                       
                        menu.variableRowHeight = true;    
                        menu.width=100;
                       
                        menu.addEventListener(MenuEvent.ITEM_CLICK, menuItemSelected);  //右键菜单的事件
                       
                        var point:Point = new Point(mouseX,mouseY); 
                       
                        point = localToGlobal(point);  
                       
                        menu.show(point.x,point.y);  //显示右键菜单
                        maxNumText.text="initMenu";
                   }
                  
                   //删除右键菜单
                  
                   private function removeMenu():void
                       
                   { 
                        if(menu!=null) 
                        { 
                             menu.hide();
                            
                             menu.removeEventListener(MenuEvent.ITEM_CLICK,menuItemSelected); 
                            
                             menu=null; 
                        } 
                        maxNumText.text="removeMenu";
                   }
                  
                   //菜单项点击事件
                  
                   private function menuItemSelected(event:MenuEvent):void
                   {
                       
                        var menuItem:Object = event.menu.selectedItem as Object;
                       
                        //……
                        switch(menuItem.label)
                            
                        {
                             case "刷新":
                                  addLine();
                                  break;
                             //                         ……
                            
                        }
                   }
                   private function addLine():void
                   {
                        maxNumText.text="addLine";
                   }
                  
                   //控件右击事件
                  
                   private function rightClickHandler(event:MouseEvent):void
                       
                   {
                        //tree_onRightClicked(event);
                        maxNumText.text="rightClickHandler0";
                        removeMenu();
                       
                        initMenu();
                        maxNumText.text="rightClickHandler";
                   }

    4.完整代码如下:
    示例代码文件
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值