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.

Recommended Answers

All 5 Replies

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.

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.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.