`
niulang85
  • 浏览: 4530 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

flex右键方案

 
阅读更多
package
{
    import flash.events.ContextMenuEvent;
    import flash.events.MouseEvent;
    import flash.ui.ContextMenuItem;
    
    import mx.core.Application;
    import mx.core.UIComponent;
    
    public class RightClickMenu 
    {
        public var MenuContents:Array = new Array ();
        
        public  function RightClickMenu(){}

        public function AddItem (name:String, func:Function):void
        {
            MenuContents.push({Name:name, Func:func});
        }

        public function AssignRightClick (uiComponent:UIComponent):void
        {
            uiComponent.addEventListener(MouseEvent.MOUSE_OVER, genEnableMenu (uiComponent));
            uiComponent.addEventListener(MouseEvent.MOUSE_MOVE, disableMenu);
        }
        
        /* Assignment */
        private function ResetContextMenu (event:MouseEvent):void
        {    //remove menu
            Application.application.contextMenu.customItems = new Array ();        
            //remove this function
            Application.application.removeEventListener(MouseEvent.MOUSE_MOVE, ResetContextMenu);        
        }
        
        private function disableMenu(event:MouseEvent):void
        {    
            //Stop the mouse move event from propagating to the application level, where we remove the menu
            event.stopImmediatePropagation();            
        }
        
        private function genEnableMenu (uiComponent:UIComponent):Function
        {                        
            return function (event:MouseEvent):void
            {        
                //add event listener to remove the menu on mouse move                
                Application.application.addEventListener(MouseEvent.MOUSE_MOVE, ResetContextMenu);            
                
                //hide current menu
                Application.application.contextMenu.hideBuiltInItems();
                
                //remove menu (ifyou right click and then move, this may not be killed.
                Application.application.contextMenu.customItems = new Array ();                
                
                //create new menu
                for (var i:Number in MenuContents)
                {
                    var menuItem:ContextMenuItem = new ContextMenuItem(MenuContents[i].Name);
                    menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, MenuContents[i].Func);                                        
                    Application.application.contextMenu.customItems.push(menuItem);    
                }    
            }            
        }
        
        private function genClickCall (func:Function):Function
        {
            return function (event:ContextMenuEvent):void
            {    
                func()
                ResetContextMenu(null);
            }

        }

    }
}

 

参考地址:http://dannygagne.com/projects/flex/context-menu-demo

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics