using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DragNDrop
{
public partial class Form1 : Form
{
//private members
private MultiPictureBox m_pic1;
private MultiPictureBox m_pic2;
private bool m_isMaximized = true;
private int m_SendingBox;
public Form1()
{
InitializeComponent();
//initialize the controls
m_pic1 = new MultiPictureBox();
m_pic1.AllowDrop = true;
m_pic1.BackColor = Color.White;
m_pic1.Location = new Point(10, 10);
m_pic1.Size = new Size(300, this.ClientSize.Height - 20);
m_pic2 = new MultiPictureBox();
m_pic2.AllowDrop = true;
m_pic2.BackColor = Color.White;
m_pic2.Location = new Point(this.ClientSize.Width - (m_pic1.Width + 10), 10);
m_pic2.Size = new Size(300, this.ClientSize.Height - 20);
this.Controls.Add(m_pic1);
this.Controls.Add(m_pic2);
//events
this.SizeChanged += new EventHandler(Form1_SizeChanged);
m_pic1.DragDrop += new DragEventHandler(m_pic_DragDrop);
m_pic1.DragEnter += new DragEventHandler(m_pic_DragEnter);
m_pic1.MouseDown += new MouseEventHandler(m_pic_MouseDown);
m_pic2.DragDrop += new DragEventHandler(m_pic_DragDrop);
m_pic2.DragEnter += new DragEventHandler(m_pic_DragEnter);
m_pic2.MouseDown += new MouseEventHandler(m_pic_MouseDown);
ImageWithBounds Pic1Blue;
ImageWithBounds Pic2Red;
ImageWithBounds Pic3Yellow;
Rectangle rectInnerBounds = new Rectangle(new Point(10, 10), new Size(20, 20));
for (int i = 0; i < 3; i++)
{
switch (i)
{
case 0:
Pic1Blue = new ImageWithBounds();
Pic1Blue.pImage = Image.FromFile("C:\\Documents and Settings\\twerner\\My Documents\\My Pictures\\icons_win\\blau_butterfly_win.ico");
Pic1Blue.pName = "blue";
Pic1Blue.pBounds = rectInnerBounds;
m_pic1.pImages.Add(Pic1Blue);
break;
case 1:
Pic2Red = new ImageWithBounds();
Pic2Red.pImage = Image.FromFile("C:\\Documents and Settings\\twerner\\My Documents\\My Pictures\\icons_win\\rot_butterfly_win.ico");
Pic2Red.pName = "red";
rectInnerBounds.Y = rectInnerBounds.Y + 100;
Pic2Red.pBounds = rectInnerBounds;
m_pic1.pImages.Add(Pic2Red);
break;
case 2:
Pic3Yellow = new ImageWithBounds();
Pic3Yellow.pImage = Image.FromFile("C:\\Documents and Settings\\twerner\\My Documents\\My Pictures\\icons_win\\gelb_butterfly_win.ico");
Pic3Yellow.pName = "yellow";
rectInnerBounds.Y = rectInnerBounds.Y + 100;
Pic3Yellow.pBounds = rectInnerBounds;
m_pic1.pImages.Add(Pic3Yellow);
break;
}
}
}
void m_pic_DragDrop(object sender, DragEventArgs e)
{
MultiPictureBox pic = (MultiPictureBox)sender;
if (pic == m_pic2 && m_SendingBox == 1)
{
m_pic2.pImages.Add((ImageWithBounds)e.Data.GetData(typeof(ImageWithBounds)));
m_pic1.pImages.Remove((ImageWithBounds)e.Data.GetData(typeof(ImageWithBounds)));
}
else
{
m_pic1.pImages.Add((ImageWithBounds)e.Data.GetData(typeof(ImageWithBounds)));
m_pic2.pImages.Remove((ImageWithBounds)e.Data.GetData(typeof(ImageWithBounds)));
}
}
void m_pic_MouseDown(object sender, MouseEventArgs e)
{
MultiPictureBox pic = (MultiPictureBox)sender;
Point pt = new Point(e.Location.X, e.Location.Y);
//is needed, in order to store, which is the target box
if (pic == m_pic1)
{
m_SendingBox = 1;
}
else
{
m_SendingBox = 2;
}
foreach (ImageWithBounds item in pic.pImages)
{
switch (item.pName)
{
case "blue":
if ((pt.X >= item.pBounds.X && pt.X <= item.pBounds.X + item.pBounds.Width)
&& (pt.Y >= item.pBounds.Y && pt.Y <= item.pBounds.Y +
item.pBounds.Height))
{
//cursor hits the blue image
pic.DoDragDrop(item, DragDropEffects.Move);
return;
}
break;
case "red":
if ((pt.X >= item.pBounds.X && pt.X <= item.pBounds.X + item.pBounds.Width)
&& (pt.Y >= item.pBounds.Y && pt.Y <= item.pBounds.Y +
item.pBounds.Height))
{
//cursor hits the red image
pic.DoDragDrop(item, DragDropEffects.Move);
return;
}
break;
case "yellow":
if ((pt.X >= item.pBounds.X && pt.X <= item.pBounds.X + item.pBounds.Width)
&& (pt.Y >= item.pBounds.Y && pt.Y <= item.pBounds.Y +
item.pBounds.Height))
{
//cursor hits the yellow image
pic.DoDragDrop(item, DragDropEffects.Move);
return;
}
break;
}
}
}
void m_pic_DragEnter(object sender, DragEventArgs e)
{
if ((MultiPictureBox)sender == m_pic2 && m_SendingBox == 1)
{
if (e.Data.GetDataPresent(typeof(ImageWithBounds)))
{
e.Effect = DragDropEffects.Move;
}
}
else if((MultiPictureBox)sender == m_pic1 && m_SendingBox == 2)
{
if (e.Data.GetDataPresent(typeof(ImageWithBounds)))
{
e.Effect = DragDropEffects.Move;
}
}
}
void Form1_SizeChanged(object sender, EventArgs e)
{
//redraw the controls if the size of the form has changed
if (m_isMaximized)
{
m_isMaximized = false;
m_pic1.Location = new Point(10, 10);
m_pic1.Size = new Size(500, this.ClientSize.Height - 20);
m_pic2.Location = new Point(this.ClientSize.Width - (m_pic1.Width + 10), 10);
m_pic2.Size = new Size(500, this.ClientSize.Height - 20);
}
else
{
m_isMaximized = true;
m_pic1.Location = new Point(10, 10);
m_pic1.Size = new Size(300, this.ClientSize.Height - 20);
m_pic2.Location = new Point(this.ClientSize.Width - (m_pic1.Width + 10), 10);
m_pic2.Size = new Size(300, this.ClientSize.Height - 20);
}
}
}
public class MultiPictureBox : Control
{
//the collection, which is used to store our images
private ImageWithBoundsCollection m_Images = new ImageWithBoundsCollection();
//read-only access to the images collection
public ImageWithBoundsCollection pImages
{
get { return m_Images; }
}
//to display the picture name or not
private bool m_bShowNames = false;
public bool pbShowNames
{
get { return m_bShowNames; }
set
{
m_bShowNames = value;
OnShowNamesChanged(EventArgs.Empty);
}
}
public MultiPictureBox()
{
//called, if the count of the image collection has changed
m_Images.CollectionChanged += new EventHandler(m_Images_CollectionChanged);
}
void m_Images_CollectionChanged(object sender, EventArgs e)
{
//the count of images has changed, so we have to redraw the control
Invalidate();
}
#region Overridden functions
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//draw the images in the multipicture box
foreach (ImageWithBounds image in m_Images)
{
if (image.pImage == null)
continue;
e.Graphics.DrawImage(image.pImage, image.pBounds);
if (m_bShowNames && !string.IsNullOrEmpty(image.pName))
{
e.Graphics.DrawString(image.pName, SystemFonts.DefaultFont, Brushes.Black,
new PointF(image.pBounds.X, image.pBounds.Y + image.pBounds.Height));
}
}
}
#endregion
protected virtual void OnShowNamesChanged(EventArgs e)
{
Invalidate();
EventHandler handler = ShowNamesChanged;
if (handler != null)
handler(this, e);
}
public event EventHandler ShowNamesChanged;
}
//this class store an image object
public class ImageWithBounds
{
private string m_Name;
public string pName
{
get { return m_Name; }
set { m_Name = value; }
}
private Image m_Image;
public Image pImage
{
get { return m_Image; }
set { m_Image = value; }
}
private Rectangle m_Bounds;
public Rectangle pBounds
{
get { return m_Bounds; }
set
{
m_Bounds = value;
}
}
}
//the collection, which stores the images of the control
public class ImageWithBoundsCollection : KeyedCollection<string, ImageWithBounds>
{
protected override string GetKeyForItem(ImageWithBounds item)
{
return item.pName;
}
protected override void InsertItem(int index, ImageWithBounds item)
{
base.InsertItem(index, item);
OnCollectionChanged(EventArgs.Empty);
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
OnCollectionChanged(EventArgs.Empty);
}
protected virtual void OnCollectionChanged(EventArgs e)
{
EventHandler handler = CollectionChanged;
if (handler != null)
handler(this, e);
}
public event EventHandler CollectionChanged;
}
}