Csharp/C Sharp by API/System.Windows.Forms/DragEventArgs

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DragEventArgs.Data.GetData

 

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;
        }
    }
}


DragEventArgs.Data.GetDataPresent

 

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;
        }
    }
}