FLEX4 屏闭系统右键菜单列表控件DataGrid/Tree/List上点右键时自动选择当前行

第一步:http://rightclickmanager.googlecode.com/下载rightclickmanager-source-lib-0.1.rar并引入自己的FLEX工程,这是实现右键点击事件,及完全屏闭系统右键菜单的关键!

第二步:打开: <主程序名>.template.html 文件修改为:

<!--
	AC_FL_runcontent(
			"src","${swf}","id","${application}","name","width","${width}","height","${height}","align","middle","menu","false","wmode","opaque",//注意:这里是右键屏蔽的关键
			"quality","high","bgcolor","${bgcolor}","allowScriptAccess","always","allowNetworking","all","allowFullScreen","true",//是否可全屏的参数
			"type","application/x-shockwave-flash","pluginspage","http://www.adobe.com/go/getflashplayer"
	);
//-->

第三步:在主程序文件.as中引入:

    import com.siloon.plugin.rightClick.RightClickManager;

 

并加如下代码

 [Event(name="rightClick",type="fla sh.events.ContextMenuEvent")]
 public class index
 {

  protected var rightClickRegisted:Boolean = false;

  public function index()
  {
   super();

   if (!rightClickRegisted)
   {
    RightClickManager.regist();
    rightClickRegisted = true;
   }
   try
   {
    name = NameUtil.createUniqueName(this);
   }
   catch (e:Error)
   {
   }
   return;

  }

  public override function toString() : String
  {
   return NameUtil.displayObjectToString(this);
  }

第四步:在要弹出右键的控件的初始化代码中加入:

NavTabTree.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);

再加以下函数来处理:

 

		/**
		 * 在控制上单击右键菜单事件的处理函数
		 * @param event 右键事件
		 * 
		 */		
		protected function rightClickHandler(event:ContextMenuEvent):void
		{
			onRightClicked(event);
			removeMenu();
			
			menu = new Menu(); 
			menu = Menu.createMenu(this,createMenuItems(),false);

			menu.labelField="label" 
			menu.variableRowHeight = true;   
			//menu.addEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler);     

			var point:Point = new Point(mouseX,mouseY);
			point = localToGlobal(point); 
			menu.show(point.x,point.y); 
		}
		/**
		 * 如果显示过Menu,则先释放资源
		 * 
		 */		
		private function removeMenu():void
		{
			if(menu!=null)
			{
				menu.hide();
				//menu.removeEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler);
				menu=null;
			}
		}
		/**
		 * 创建右键菜单选项
		 * @return 返回选项组成的Array
		 * 
		 */		
		private function createMenuItems():Array
		{
			var menuItems:Array = new Array();
			var menuItem:Object; 

			menuItem = new Object;
			menuItem.label = '清空画布';
			menuItem.action = 'clearall';
			menuItems.push(menuItem); 

			menuItem = new Object;
			menuItem.label = '选择背景';
			menuItem.action = 'selectBackImage';
			menuItems.push(menuItem); 

			return menuItems;
		}
		/**
		 * 当右键单击Tree的Item时,自动选择鼠标所在的当前项
		 * @param e 自定义右键菜单事件
		 * 
		 */
		private function onRightClicked(e:ContextMenuEvent):void

		{

			var rightClickItemRender:IListItemRenderer;  

			var rightClickIndex:int;

			if(e.mouseTarget is IListItemRenderer) {  

				rightClickItemRender = IListItemRenderer(e.mouseTarget);  

			}else if(e.mouseTarget.parent is IListItemRenderer) {  

				rightClickItemRender = IListItemRenderer(e.mouseTarget.parent);  

			}  



			if(rightClickItemRender != null) {  

				rightClickIndex = NavTabTree.itemRendererToIndex(rightClickItemRender);  

				if(NavTabTree.selectedindex != rightClickIndex) {  

					NavTabTree.selectedindex = rightClickIndex; 

				} 

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


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

第五步:

修改RightClickManager.as文件

static private var rightClickTarget:*;

修改两个该文件的两个函数

       

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


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

   到这里,功能就完成了,本人修改了RightClickManager的事件从MouseEvent改为ContextMenuEvent是为了能在列表控件DataGrid/Tree/List上点右键时自动选择当前行,使用了ContextMenuEvent事件中的event.mouseTarget和列表控件的IListItemRenderer接口!

        本功能也有副作用,系统右键菜单失效,那文本的右键复制粘贴也就失效了,有大量用到TEXTINPUT的就要小心使用了~!

相关文章

一:display:flex布局display:flex是一种布局方式。它即可以...
1. flex设置元素垂直居中对齐在之前的一篇文章中记载过如何...
移动端开发知识点pc端软件和移动端apppc端软件是什么,有哪些...
最近挺忙的,准备考试,还有其他的事,没时间研究东西,快周...
display:flex;把容器设置为弹性盒模型(设置为弹性盒模型之后...
我在网页上运行了一个Flex应用程序,我想使用Command←组合键...