using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
private static extern bool DestroyIcon(IntPtr handle);
private ListViewItem lvi;
private IntPtr hIcon = IntPtr.Zero;
private class ImageDrop
{
public Point Location;
public int ImageIndex;
}
private List Drops = new List();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < imageList1.Images.Count; i++)
{
listView1.Items.Add("Item " + i.ToString(), i);
}
listView1.AllowDrop = true;
pictureBox1.AllowDrop = true;
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lvi = listView1.GetItemAt(e.X, e.Y);
if (lvi != null)
{
listView1.DoDragDrop(lvi, DragDropEffects.All);
}
}
}
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
if (!hIcon.Equals(IntPtr.Zero))
{
DestroyIcon(hIcon);
}
Bitmap bmp = new Bitmap(imageList1.Images[lvi.ImageIndex]);
hIcon = bmp.GetHicon();
Cursor.Current = new Cursor(hIcon);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
Point pt = pictureBox1.PointToClient(new Point(e.X, e.Y));
ImageDrop id = new ImageDrop();
id.Location = pt;
id.ImageIndex = lvi.ImageIndex;
Drops.Add(id);
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (ImageDrop id in Drops)
{
e.Graphics.DrawImage(imageList1.Images[id.ImageIndex], id.Location);
}
}
}
}
|