본문 바로가기

카테고리 없음

ContextMenu Demo

Label 콤포넌트 위에서 마우스 우측을 클릭했을 때 팝업메뉴(ContextMenu)를 보여주는 예

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
 initialize="init();">
 
 <mx:Script>
  <![CDATA[
 
   private function init():void {
   
    var contextMenu:ContextMenu = new ContextMenu();
    contextMenu.hideBuiltInItems();

    var menuItem:ContextMenuItem = new ContextMenuItem("색상변경");
    contextMenu.customItems.push(menuItem);

    myLabel.contextMenu = contextMenu;
   }
  ]]>
 </mx:Script>

 <mx:Label text="여기에서 마우스 우측 클릭" fontSize="15" horizontalCenter="0" id="myLabel"/>
 
</mx:Application>



ContextMenu의 아이템을 선택했을 때 실행되는 이벤트핸들러를 설정하여 Label의 글자색상을 변경하는 예

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
 initialize="init();">
 
 <mx:Script>
  <![CDATA[
 
   function init():void {
    
    var contextMenu:ContextMenu = new ContextMenu();
    contextMenu.hideBuiltInItems();

    var menuItem:ContextMenuItem = new ContextMenuItem("색상변경");
    menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,changeColor);
    contextMenu.customItems.push(menuItem);

    myLabel.contextMenu = contextMenu;
    
   }
   
   function changeColor(event:ContextMenuEvent):void {
    var red:uint = 0xFF0000;
    var def:uint = 0x0B333C; // 시스템의 디폴트 색상

    var color:uint = myLabel.getStyle("color");
    if(color==red) myLabel.setStyle("color", def);
    else if(color==def) myLabel.setStyle("color", red);
   }
  ]]>
 </mx:Script>

 <mx:Label text="여기에서 마우스 우측 클릭" fontSize="15" horizontalCenter="0" id="myLabel"/>
 
</mx:Application>