2012-02-12 60 views
1

我很難得到一個contextmenu或某種形式的NativeMenu,在我的AIR應用程序中點擊右鍵。有人能指出我正確的方向還是提供一小段示例代碼,所以我在腦海中澄清它?Flex中的右鍵菜單AIR

謝謝!

回答

0

當然,

看看這個:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/ContextMenu.html

這裏是例子:

package { 
    import flash.ui.ContextMenu; 
    import flash.ui.ContextMenuItem; 
    import flash.ui.ContextMenuBuiltInItems; 
    import flash.events.ContextMenuEvent; 
    import flash.display.Sprite; 
    import flash.display.Shape; 
    import flash.text.TextField; 

    public class ContextMenuExample extends Sprite { 
     private var myContextMenu:ContextMenu; 
     private var menuLabel:String = "Reverse Colors"; 
     private var textLabel:String = "Right Click"; 
     private var redRectangle:Sprite; 
     private var label:TextField; 
     private var size:uint = 100; 
     private var black:uint = 0x000000; 
     private var red:uint = 0xFF0000; 

     public function ContextMenuExample() { 
      myContextMenu = new ContextMenu(); 
      removeDefaultItems(); 
      addCustomMenuItems(); 
      myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler); 

      addChildren(); 
      redRectangle.contextMenu = myContextMenu; 
     } 

     private function addChildren():void { 
      redRectangle = new Sprite(); 
      redRectangle.graphics.beginFill(red); 
      redRectangle.graphics.drawRect(0, 0, size, size); 
      addChild(redRectangle); 
      redRectangle.x = size; 
      redRectangle.y = size; 
      label = createLabel(); 
      redRectangle.addChild(label); 
     } 

     private function removeDefaultItems():void { 
      myContextMenu.hideBuiltInItems(); 
      var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems; 
      defaultItems.print = true; 
     } 

     private function addCustomMenuItems():void { 
      var item:ContextMenuItem = new ContextMenuItem(menuLabel); 
      myContextMenu.customItems.push(item); 
      item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler); 
     } 

     private function menuSelectHandler(event:ContextMenuEvent):void { 
      trace("menuSelectHandler: " + event); 
     } 

     private function menuItemSelectHandler(event:ContextMenuEvent):void { 
      trace("menuItemSelectHandler: " + event); 
      var textColor:uint = (label.textColor == black) ? red : black; 
      var bgColor:uint = (label.textColor == black) ? black : red; 
      redRectangle.graphics.clear(); 
      redRectangle.graphics.beginFill(bgColor); 
      redRectangle.graphics.drawRect(0, 0, size, size); 
      label.textColor = textColor; 
     } 

     private function createLabel():TextField { 
      var txtField:TextField = new TextField(); 
      txtField.text = textLabel; 
      return txtField; 
     } 
    } 
}