Question : Moving an image along side the Mouse Pointer moves

Hello Experts,

I am developing a windows application using C#.NET. I have 2 controls on one usercontrol (yes in windows application for loading it into the panel control of the MDIParent Form). One is PictureBox & second one is ListView control & latter is populated with some images in it.

Now what I want to achieve is that I want to drag & drop images from ListView to picture box control. And while dragging I want to move image itself along side the mouse pointer. How can I achieve this?

Any help would be appreciated. Thanks !!!

Answer : Moving an image along side the Mouse Pointer moves

I'm not sure what the difference is.  I added a ListView, ImageList and PictureBox to a Form.  Then I manually added a couple images to my ImageList.  The sample code below loads up the Listview with the images I loaded up.

When I run it and drag one of the ListView items I see the actual image during the entire operation.  I've added some code to make the image "persistent" in the PictureBox.

I wish I could show a video of what I see but unfortunately CamStudio doesn't capture the custom cursor as I see it.  =\

Anyhoo, here is the code I'm testing with.  In the screenshot below the PictureBox is the bottom box and I have dropped several of the ListViewItems on it already:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
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);
            }
        } 
    }
}
 
I see the custom cursor during the ENTIRE drag...
I see the custom cursor during the ENTIRE drag...
 
Random Solutions  
 
programming4us programming4us