Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

wpf - Drag and Drop: How to show "Moving" cursor while CTRL is pressed?

I have a custom control that works like a Canvas. I'm trying to launch a "move" operation via DragDrop.DoDragDrop() (in MouseLeftButtonDown event handler). However, I require the user to press CTRL key during mouse down, because I have a separate use-case which the user needs to left click without pressing CTRL key.

I tried passing in only DragDropEffects.Move to DoDragDrop() but holding the CTRL key would visually show a cross ("not allowed") cursor. Using DragDropEffects.Move | DragDropEffects.Copy would show a cursor that looks like combination of the two operations.

Handling the GiveFeedback event isn't useful either - I need to set a custom cursor, or use one of the system cursors which do not include the move cursor.

How do I make it show the move cursor during drag and drop?

question from:https://stackoverflow.com/questions/65838762/drag-and-drop-how-to-show-moving-cursor-while-ctrl-is-pressed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code with custom cursor loaded from resource works for me.

private Cursor customCursor = null;

private void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataObject data = new DataObject(DataFormats.Text, <data>);
    DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy | DragDropEffects.Move);
}

private void Element_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    if (e.Effects == DragDropEffects.Copy || e.Effects == DragDropEffects.Move)
    {
        if (customCursor == null)
        {
            var rs = Application.GetResourceStream(new Uri("move.cur", UriKind.Relative));
            customCursor = new Cursor(rs.Stream);
        }

        e.UseDefaultCursors = false;
        Mouse.SetCursor(customCursor);
    }
    else
        e.UseDefaultCursors = true;
        e.Handled = true;
}

Holding the Ctrl key during a drag and drop operation relates to copying the item and therefore you need to allow the DragDropEffects.Copy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...