2007-06-27

利用 NativeDragEvent, 以拖拉放的方式開啟檔案

參考網頁: Simple Drag and Drop AIR
依據上一篇: Adobe AIR Simple Text Editor 再加上拖曳開檔的功能
Flash frame actions:
import flash.events.NativeDragEvent;
import flash.desktop.DragManager;
import flash.desktop.DragActions;
import flash.desktop.TransferableFormats;
import flash.desktop.TransferableData;

this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
this.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT,onDragExit);

function onDragIn(event:NativeDragEvent):void {
// acceptDragDrop() 只能在 nativeDragEnter 或 nativeDragOver 事件處理器上使用
DragManager.acceptDragDrop(this);
}

function onDrop(event:NativeDragEvent):void {

// 下行若沒設定, 順序會是 copy, move, link
DragManager.dropAction = DragActions.COPY;
var tfd:TransferableData = event.transferable as TransferableData;
var dropfiles:Array = tfd.dataForFormat(TransferableFormats.FILE_LIST_FORMAT) as Array;
// 只取一個檔案
var sourceFile:File = dropfiles[0];
file = new File();
file.nativePath = sourceFile.nativePath;
filename_txt.text = file.nativePath;

fs = new FileStream();
fs.open(file, FileMode.READ);
var str:String = fs.readMultiByte(fs.bytesAvailable, "utf-8");
main_txt.text = str.replace( new RegExp(File.lineEnding, "g"), "\n");
fs.close();
}
function onDragExit(event:NativeDragEvent):void {
trace("Drag exit event.");
}

原始檔: test_dragAndDrop.fla

沒有留言:

FB 留言