Resolved Using a Custom Cursor with Drag and Drop Flickers

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
95
Programming Experience
10+
Hi,
I am trying to use my PictureBox image as a Custom Cursor for Drag and Drop. From the documentation, it suggests that I set the cursor in the GiveFeedback routine. If I do that, then the Image flickers. Is there a different or more preferred method to do this?

C#:
private void pbLessThanLg_MouseDown(object sender, MouseEventArgs e)
{
    PictureBox pbSelected = (PictureBox)sender;
    pbSelected.DoDragDrop(pbSelected.Image, DragDropEffects.Copy);
}

private void pbLessThanLg_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    PictureBox pbSelected = (PictureBox)sender;
    Cursor.Current = BitmapToCursor((Bitmap)pbSelected.Image, 64, 64);
}
 
Solution
In general, you should only change the cursor when the feedback event args indicate that you are changing states from copy to move, or from move to no-drop, etc. Otherwise if the current drag-drop state is the same as the last drag-drop state, leave things alone.

Also, is your BitmapToCursor() creating a brand new cursor each time? You should try to cache cursors and only create new ones when needed.

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
In general, you should only change the cursor when the feedback event args indicate that you are changing states from copy to move, or from move to no-drop, etc. Otherwise if the current drag-drop state is the same as the last drag-drop state, leave things alone.

Also, is your BitmapToCursor() creating a brand new cursor each time? You should try to cache cursors and only create new ones when needed.
 
Solution

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
95
Programming Experience
10+
In general, you should only change the cursor when the feedback event args indicate that you are changing states from copy to move, or from move to no-drop, etc. Otherwise if the current drag-drop state is the same as the last drag-drop state, leave things alone.

Also, is your BitmapToCursor() creating a brand new cursor each time? You should try to cache cursors and only create new ones when needed.

Sky,
Thanks. I changed the GiveFeedback to:
C#:
private void pbLessThanLg_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    if (e.Effect == DragDropEffects.Copy)
    {
        e.UseDefaultCursors = false;
        Cursor.Current = pbLessThanLgCursor;
    }
    else
        e.UseDefaultCursors = false;
}

and it worked perfectly!
 
Top Bottom