954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Ghost Drag and Drop

Hi; In need of some guidance. I have a form which has a list box upon it. with items in it. These items are dragged out of this and a custom control is added to the form. All this works fine. However a new requirement is that there should be a ghost copy to follow the mouse when this is conducted.

I have managed to create a new Cursor which is generated dynamically by creating a bitmap of the control, and then adding this through the CustomCursor.CreatCursor() method. When the item is initially clicked in the lstJobs list box the cursor changes.

This is all fine until....

You drag the item away from this list box on to the rest of the form; which has worked till this reguirement through:

lstjobs.DoDragDrop(JobID,DragDropEffects.All)


to set the cursor I am using the following:

RectangleF RecF = new RectangleF();
            RecF.Height = RecF.Height + OC.Height;
            Bitmap memoryImage = new Bitmap(OC.Width, OC.Height);
            OC.DrawToBitmap(memoryImage, new Rectangle(new Point(), OC.Size));


            Graphics G = Graphics.FromImage(memoryImage);


            using (Font f = new Font(FontFamily.GenericSansSerif, 10))
                G.DrawString("Drop on form", f, Brushes.Green, 0, 0);


            Cursor.Current = CustomCursor.CreateCursor(memoryImage, 3, 3);
            this.Cursor = CustomCursor.CreateCursor(memoryImage, 3, 3);
            memoryImage.Dispose();


Where the class CustomCursor is generated as follows:

public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }

    public static class CustomCursor
    {
        [DllImport("user32.dll")]
        public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

        public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            IconInfo tmp = new IconInfo();
            GetIconInfo(bmp.GetHicon(), ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            return new Cursor(CreateIconIndirect(ref tmp));
        }
    }

So running with out break points makes it appear that nothing changes... how ever when you had breakpoints in it appears that when Events are firing such as drag enter, drag over then the cursor is getting reset. any ideas?

Thanks in advance.

taylby
Newbie Poster
24 posts since Feb 2010
Reputation Points: 10
Solved Threads: 2
 

You cannot break in to a Drag-Drop event and then continue it.
This is because the focus is passed to the devEnv and the drag-drop ends.
Instead, put Debug.WriteLine commands in your code to get diagnostic info on what is happening.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Thanks Nick, unless I am wrong in reading your reply my problem isn't with the break points changing the data structure, having untold results. I feel the problem being that unless the breakpoint is on, you can not visually see the change made to the cursor. It is almost as if there is a refresh rate that is too high.
ON the following piece of code I am making a call the the method I have to set the cursor.

private void LoadJobCentre_DragEnter(object sender, DragEventArgs e)
        {


            e.Effect = DragDropEffects.Move;
            //--------------------------------
           
            SetCursorToOpsCentre();

        
        }



On this drag enter event it just keeps iterating around while the mouse button is held down, thus reloading the cursor on each turn. Any ideas? And once again thanks.

taylby
Newbie Poster
24 posts since Feb 2010
Reputation Points: 10
Solved Threads: 2
 
I feel the problem being that unless the breakpoint is on, you can not visually see the change made to the cursor.

Do you mean that the drag-drop only works if breakpoints are set?
What happens when you have no breakpoints?
Is it just the cursor changing that is the problem or do other drag-drop events not fire?

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Hi Nick; I was meaning that if the breakpoints where not set then the ghost cursor was not visible, the drag-drop events worked fine; it was just a visual. I have resolved the issue now though through the following code:

private void lstJobs_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            if (e.Effect == DragDropEffects.Move)
            {
                e.UseDefaultCursors = false;
                this.Cursor = SetCursorToOpsCentre();
                
            }
            else
            {
                e.UseDefaultCursors = true;
                
            }
        }

which does the same thing that I had earlier just in the appropriate place. Thanks for looking over the issue.

taylby
Newbie Poster
24 posts since Feb 2010
Reputation Points: 10
Solved Threads: 2
 

This is an interesting solution, I hacked up something to do this one time example here http://www.daniweb.com/forums/thread194234.html . But I like your way of handling it a bit better.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: