AIR Drag and Drop 学习笔记(20080303)
Author: cbm
3 Mar 2008
昨天学习到File类和FileStream类,操作起来很方便的,本准备用AIR技术制作一个文件夹图标修改器 ,但遗憾的是并没有提供设置文件属性的方法。这样修改文件夹图标的关键技术不能实现了。
今天看了一下拖拽类,主要由NativeDragEvent来监听鼠标行为,用 Clipboard.hasFormat() 来判断是否是支持的文件类型。然后用NativeDragManager.acceptDragDrop(dragTarget)来控制拖拽对象的鼠标显 示状态。在NativeDragEvent.NATIVE_DRAG_DROP事件处理拖拽的文件。可以按不同类型的文件来处理。内置文件类型
BITMAP_FORMAT FILE_LIST_FORMAT HTML_FORMAT TEXT_FORMAT URL_FORMAT
贴上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 package { import flash.desktop.*; import flash.display.Sprite; import flash.events.*; import flash.filesystem.File; import flash.text.TextField; public class DragOut_test extends Sprite { private var dragTarget:Sprite = new Sprite( ) ; private var backgroundColor:Number = 0x0097CA; private var consoleText:TextField = new TextField ( ) public function DragOut_test( ) { this .addChild ( consoleText) this .addChild ( dragTarget) dragTarget.focusRect = false ; stage.stageFocusRect = false ; drawBackground( ) //注意stage上必须要有可见元素,组成检测区域。 configConsoleText( ) //创建输出文本框 dragTarget.addEventListener ( NativeDragEvent.NATIVE_DRAG_ENTER ,onDragIn) ;//通常做拖入文件的类型检查 dragTarget.addEventListener ( NativeDragEvent.NATIVE_DRAG_DROP ,onDrop) ;//拖拽完成事件 dragTarget.addEventListener ( NativeDragEvent.NATIVE_DRAG_EXIT ,onDragExit) ;//拖拽取消 } private function drawBackground( ) :void { dragTarget.graphics .clear ( ) ; dragTarget.graphics .beginFill ( backgroundColor,0 ) ; dragTarget.graphics .drawRect ( 0 ,0 ,stage.stageWidth ,stage.stageHeight ) ; dragTarget.graphics .endFill ( ) ; dragTarget.hitArea =null } private function configConsoleText( ) { consoleText.width =stage.stageWidth consoleText.height =stage.stageHeight consoleText.selectable =false consoleText.multiline =true consoleText.appendText ( '请拖拽一个或者多个文件到此!/n ' ) } private function onDragIn( event:NativeDragEvent) :void { var transferable:Clipboard = event.clipboard ; if ( transferable.hasFormat ( ClipboardFormats.BITMAP_FORMAT ) || transferable.hasFormat ( ClipboardFormats.FILE_LIST_FORMAT ) || transferable.hasFormat ( ClipboardFormats.TEXT_FORMAT ) || transferable.hasFormat ( ClipboardFormats.URL_FORMAT ) ) { consoleText.appendText ( "接受的格式。/n " ) NativeDragManager.acceptDragDrop ( dragTarget) ; } else { consoleText.appendText ( "不接受的格式。/n " ) } } private function onDrop( event:NativeDragEvent) :void { var dropfiles:Array = event.clipboard .getData ( ClipboardFormats.FILE_LIST_FORMAT ) as Array ; for each ( var file:File in dropfiles) { consoleText.appendText ( "拖入文件:" +file.nativePath +'/n ' ) } } private function onDragExit( event:NativeDragEvent) :void { consoleText.appendText ( "拖拽退出。/n " ) } } }
AIR Drag and Drop 学习笔记(20080302) (7.7 KB, 16 hits)