Drag and Drop Image, Copy Move : Image Drag Drop « Graphics « Flex






Drag and Drop Image, Copy Move

Drag and Drop Image, Copy Move
           


<!--
Code from Flex 4 Documentation "Using Adobe Flex 4".

This user guide is licensed for use under the terms of the Creative Commons Attribution 
Non-Commercial 3.0 License. 

This License allows users to copy, distribute, and transmit the user guide for noncommercial 
purposes only so long as 
  (1) proper attribution to Adobe is given as the owner of the user guide; and 
  (2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms. 
The best way to provide notice is to include the following link. 
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

-->



    <!-- dragdrop\DandDImageCopyMove.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:HorizontalLayout />
    </s:layout>
    <fx:Script> 
         
        import mx.managers.DragManager; 
        import mx.core.DragSource; 
        import mx.events.DragEvent; 
        import flash.events.MouseEvent; 
        // Embed icon image. 
        [Embed(source='a.jpg')] 
        public var globeImage:Class; 
        // The mouseMove event handler for the Image control 
        // functioning as the drag initiator. 
        private function mouseOverHandler(event:MouseEvent):void 
        { 
            var dragInitiator:Image=Image(event.currentTarget); 
            var ds:DragSource = new DragSource(); 
            ds.addData(dragInitiator, "img"); 
            // The drag manager uses the image as the drag indicator 
            // and sets the alpha to 1.0 (opaque), 
            // so it appears to be dragged across the canvas. 
            var imageProxy:Image = new Image(); 
            imageProxy.source = globeImage; 
            imageProxy.height=10; 
            imageProxy.width=10; 
            DragManager.doDrag(dragInitiator, ds, event, 
            imageProxy, -15, -15, 1.00); 
        } 
        // The dragEnter event handler for the Canvas container 
        // functioning as the drop target. 
        private function dragEnterHandler(event:DragEvent):void { 
            if (event.dragSource.hasFormat("img")) 
                DragManager.acceptDragDrop(Canvas(event.currentTarget)); 
        } 
        // The dragOver event handler for the Canvas container 
        // sets the type of drag-and-drop 
        // operation as either copy or move. 
        // This information is then used in the 
        // dragComplete event handler for the source Canvas container. 
        private function dragOverHandler(event:DragEvent):void 
        { 
            if (event.dragSource.hasFormat("img")) { 
                if (event.ctrlKey) { 
                    DragManager.showFeedback(DragManager.COPY); 
                    return; 
                } else { 
                    DragManager.showFeedback(DragManager.MOVE); 
                    return; 
                } 
            } 
            DragManager.showFeedback(DragManager.NONE); 
        } 
        // The dragDrop event handler for the Canvas container 
        // sets the Image control's position by 
        // "dropping" it in its new location. 
        private function dragDropHandler(event:DragEvent):void { 
            if (event.dragSource.hasFormat("img")) { 
                var draggedImage:Image = event.dragSource.dataForFormat('img') as Image; 
                var dropCanvas:Canvas = event.currentTarget as Canvas; 
                // Since this is a copy, create a new object to 
                // add to the drop target. 
                var newImage:Image=new Image(); 
                newImage.source = draggedImage.source; 
                newImage.x = dropCanvas.mouseX; 
                newImage.y = dropCanvas.mouseY; 
                dropCanvas.addChild(newImage); 
            } 
        } 
        // The dragComplete event handler for the source Canvas container 
        // determines if this was a copy or move. 
        // If a move, remove the dragged image from the Canvas. 
        private function dragCompleteHandler(event:DragEvent):void { 
            var draggedImage:Image = event.dragInitiator as Image; 
            var dragInitCanvas:Canvas = event.dragInitiator.parent as Canvas; 
            if (event.action == DragManager.MOVE) 
                dragInitCanvas.removeChild(draggedImage); 
        } 
      
    </fx:Script>
    <!-- Canvas holding the Image control that is the drag initiator. -->
    <mx:Canvas width="250" height="500" borderStyle="solid"
        backgroundColor="#DDDDDD">
        <!-- The Image control is the drag initiator and the drag indicator. -->
        <mx:Image id="myimg" source="@Embed(source='a.jpg')"
            mouseMove="mouseOverHandler(event);" dragComplete="dragCompleteHandler(event);" />
    </mx:Canvas>
    <!-- This Canvas is the drop target. -->
    <mx:Canvas width="250" height="500" borderStyle="solid"
        backgroundColor="#DDDDDD" dragEnter="dragEnterHandler(event);"
        dragOver="dragOverHandler(event);" dragDrop="dragDropHandler(event);">
    </mx:Canvas>
</s:Application>

   
    
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Creating a custom drag imageCreating a custom drag image
2.Drag and drop image on Canvas with ghost imageDrag and drop image on Canvas with ghost image
3.Drag and drop image on CanvasDrag and drop image on Canvas
4.Drag to copy image between two Canvas controlsDrag to copy image between two Canvas controls
5.Drag and move an Image from one Canvas to another CanvasDrag and move an Image from one Canvas to another Canvas
6.Use Image control to load a draggable image into a Canvas container.Use Image control to load a draggable image into a Canvas container.