Flex 实现组件拖曳(二)

 前面的例子从一个地方移动数据到另一个地方使用了缺省设置,但你也可以很容易地建立 处

理过程来复制那些数据。接下来的例子复制了一个可拖拽的 Box 组件的信息给一个拖拽源
( DragSource )对象,这个对象用来创建一个新的 Box 实例,然后将这个 Box 实例增加到 释
放目标 (drop target) 的显示清单中。
下面有两种形式:1、是增加一新的组件。2、拖动的还是原来的组件。
 
Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="100%" height="100%">  
  5.     <s:layout>  
  6.         <s:HorizontalLayout verticalAlign="middle"/>  
  7.     </s:layout>  
  8.     <fx:Declarations>  
  9.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  10.     </fx:Declarations>  
  11.       
  12.     <fx:Script>  
  13.         <![CDATA[ 
  14.             import mx.core.DragSource; 
  15.             import mx.core.IUIComponent; 
  16.             import mx.events.DragEvent; 
  17.             import mx.managers.DragManager; 
  18.             private static const FORMAT:String = "box"; 
  19.              
  20.             private function mouseDownHandle(evt:MouseEvent):void { 
  21.                  
  22.                 var initiator:Box = evt.currentTarget as Box; 
  23.                 var boxData:Object = new Object(); 
  24.                  
  25.                 boxData.width = initiator.width; 
  26.                 boxData.height = initiator.height; 
  27.                 boxData.backgroundColor = initiator.getStyle("backgroundColor"); 
  28.                 //DragSource 类中包含正被拖动的数据 
  29.                 var dragSource:DragSource = new DragSource(); 
  30.                 //addData(data:Object, format:String):void向拖动源添加数据和相应的格式 String。 
  31.                 dragSource.addData(boxData, FORMAT); 
  32.                 //启动拖放操作。 
  33.                 //dragInitiator:IUIComponent — 指定启动拖动的组件的 IUIComponent。 
  34.                 //dragSource:DragSource — 包含正在拖动的数据的 DragSource 对象。 
  35.                 //mouseEvent:MouseEvent — 包含与启动拖动相关的鼠标信息的 MouseEvent 
  36.                 DragManager.doDrag(initiator, dragSource, evt); 
  37.             } 
  38.              
  39.             private function dragEnterHandle(evt:DragEvent):void { 
  40.                 //如果数据源中包含请求的格式,则返回 true;否则,返回 false。 
  41.                 if(evt.dragSource.hasFormat(FORMAT)) {           
  42.                     DragManager.acceptDragDrop(Canvas(evt.currentTarget)); 
  43.                 } 
  44.             } 
  45.             private function dropHandle(evt:DragEvent):void {    
  46.                 //确定拖动后的位置 
  47.                 var boxData:Object = evt.dragSource.dataForFormat(FORMAT); 
  48. //              var box:Box = new Box(); 
  49. //              box.width = boxData.width; 
  50. //              box.height = boxData.height; 
  51. //              box.setStyle("backgroundColor", boxData.backgroundColor); 
  52. //               
  53. //              box.x = evt.localX; 
  54. //              box.y = evt.localY; 
  55.                  
  56.                 dragItem.width = boxData.width; 
  57.                 dragItem.height = boxData.height; 
  58.                 dragItem.setStyle("backgroundColor", boxData.backgroundColor); 
  59.                  
  60.                 dragItem.x = evt.localX; 
  61.                 dragItem.y = evt.localY; 
  62.                  
  63. //              canvas.addChild(box); 
  64.             } 
  65.         ]]>  
  66.     </fx:Script>  
  67.     <mx:Canvas id="canvas"  
  68.                backgroundColor="0xEEEEEE"  
  69.                width="80%" height="80%"  
  70.                dragEnter="dragEnterHandle(event);"  
  71.                dragDrop="dropHandle(event);">  
  72.         <mx:Box id="dragItem"   
  73.                 width="20%" height="20%"  
  74.                 backgroundColor="0x00FFCC"  
  75.                 mouseDown="mouseDownHandle(event);" />  
  76.     </mx:Canvas>  
  77.       
  78. </s:Application>  
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值