using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace picImgDropMoveTest
{
public partial class Form1 : Form
{
PictureBox pictureBox1 = new PictureBox();
PictureBox pictureBox2 = new PictureBox();
Bitmap bm;
Metafile mf;//
public Form1()
{
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
pictureBox1.Location = new Point(10, 20);
pictureBox1.Size = new Size(200, 200);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.DragDrop += _DragDrop;
pictureBox1.MouseDown += pic_MD;
pictureBox1.DragEnter += _DragEnter;
pictureBox2.BorderStyle = BorderStyle.FixedSingle;
pictureBox2.Location = new Point(220, 20);
pictureBox2.Size = new Size(200, 200);
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.DragDrop += pictureBox2_DragDrop;
pictureBox2.DragEnter += pictureBox2_DragEnter;
this.ClientSize = new Size(440, 240);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
}
private void _DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void _DragDrop(object sender, DragEventArgs e)
{
string[] FilePath;
FilePath = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string fname = FilePath.Length > 0 ? FilePath[0] : "";
if ("" == fname) return;
pictureBox1.LoadAsync(fname);
}
private void pic_MD(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
bm = pictureBox1.Image as Bitmap;
mf = pictureBox1.Image as Metafile;//
if ((bm == null && mf == null) || bm == pictureBox1.ErrorImage) return;//if (bm == null|| bm == pictureBox1.ErrorImage) return;
DataObject dataObj = bm != null ? new DataObject(DataFormats.Bitmap, bm) : new DataObject(DataFormats.MetafilePict, mf);// DataObject dataObj = new DataObject(DataFormats.Bitmap, bm);
DragDropEffects dde = pictureBox1.DoDragDrop(dataObj, DragDropEffects.Copy);
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap) || e.Data.GetDataPresent(DataFormats.MetafilePict))// if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
if (bm == null && mf == null) return;//if (bm == null) return;
if (bm != null) pictureBox2.Image = bm; else pictureBox2.Image = mf;//pictureBox2.Image = bm;
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}