Image Drop : Drag Drop « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » Drag DropScreenshots 
Image Drop
 

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class ImageDrop : Form {
    bool bIsTarget;
    Image image;
    public static void Main() {
        Application.Run(new ImageDrop());
    }
    public ImageDrop() {
        AllowDrop = true;
    }
    protected override void OnDragOver(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop|| dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) {
            if ((dea.AllowedEffect & DragDropEffects.Move!= 0)
                dea.Effect = DragDropEffects.Move;
            if (((dea.AllowedEffect & DragDropEffects.Copy!= 0&& ((dea.KeyState & 0x08!= 0))    // Ctrl key
                dea.Effect = DragDropEffects.Copy;
        }
    }
    protected override void OnDragDrop(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop)) {
            string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop);
            image = Image.FromFile(astr[0]);
            Invalidate();
        else {
            if (dea.Data.GetDataPresent(typeof(Metafile)))
                image = (Image)dea.Data.GetData(typeof(Metafile));
            else if (dea.Data.GetDataPresent(typeof(Bitmap)))
                image = (Image)dea.Data.GetData(typeof(Bitmap));
            bIsTarget = true;
            Invalidate();
        }
    }
    protected override void OnMouseDown(MouseEventArgs mea) {
        if (image != null) {
            bIsTarget = false;
            DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move);
            if (dde == DragDropEffects.Move && !bIsTarget)
                image = null;
        }
    }
}

 
Related examples in the same category
1.Drag and drop inside a containerDrag and drop inside a container
2.Drag and drop image to another windowDrag and drop image to another window
3.Drag and drop the PictureBoxDrag and drop the PictureBox
4.Fake Drag And DropFake Drag And Drop
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.