论坛首页 编程语言技术论坛

Flex拖拽-Canvas容器里拖动Box

浏览 5804 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-18   最后修改:2009-02-18
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.core.IUIComponent;
			import mx.containers.Box;
			import mx.containers.Canvas;
			import mx.events.DragEvent;
			import mx.managers.DragManager;
			import mx.core.DragSource;
			import mx.core.UIComponent;
			private static const FORMAT:String = "box";

			private function boxMouseDownHandler(evt:MouseEvent):void{
				var iu:IUIComponent = evt.currentTarget as IUIComponent;
				var dragSource:DragSource = new DragSource();
				dragSource.addData( iu, FORMAT );
				DragManager.doDrag( iu, dragSource, evt );
			}
			
			private function canvasDragEnterHandler(evt:DragEvent):void{
				if(evt.dragSource.hasFormat(FORMAT)){
					DragManager.acceptDragDrop(Canvas(evt.currentTarget));
				}
			}
			
			private function canvasDragHandler(evt:DragEvent):void{
				var box:Box = Box(evt.dragInitiator);
				box.x = evt.localX;
				box.y = evt.localY;
			}
		]]>
	</mx:Script>
	
	<mx:Canvas backgroundColor="0xEEEEEE" width="500" height="200" horizontalCenter="0" verticalCenter="0" dragEnter="canvasDragEnterHandler(event)" dragDrop="canvasDragHandler(event)">
		<mx:Box width="20" height="20" backgroundColor="0x00FFCC" x="137" y="66" mouseDown="boxMouseDownHandler(event);"></mx:Box>
	</mx:Canvas>
</mx:WindowedApplication>

 

 

当<mx:Box>发出mouseDown事件时,boxMouseDownHandler 方法被调用并且拖拽源数据被增加到DragManager。
canvasDragEnterHandler检查拖拽源的格式
释放时canvasDragHandler方法改变拖拽源的位置

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics