DragDropEffects 枚举

定义

指定拖放操作的可能效果。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum DragDropEffects
继承
DragDropEffects
属性

字段

All -2147483645

CopyMoveScroll 效果的组合。

Copy 1

将拖动源中的数据复制到放置目标。

4

将拖动源中的数据链接到放置目标。

Move 2

将拖动源的数据移动到放置目标。

None 0

放置目标不接受该数据。

Scroll -2147483648

拖动时可以滚动目标,以定位在目标中当前不可见的某个放置位置。

示例

以下示例演示 DragDropEffects 当用户在拖放操作期间将鼠标移到放置目标上时使用枚举。 此示例是为 Control.DoDragDrop 该方法提供的大型示例的一部分。

C#
private void ListDragTarget_DragOver(object sender, DragEventArgs e)
{
    // Determine whether string data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.
    if (!e.Data.GetDataPresent(typeof(System.String)))
    {
        e.Effect = DragDropEffects.None;
        DropLocationLabel.Text = "None - no string data.";
        return;
    }

    // Set the effect based upon the KeyState.
    if ((e.KeyState & (8 + 32)) == (8 + 32) &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // KeyState 8 + 32 = CTRL + ALT

        // Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 32) == 32 &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // ALT KeyState for link.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 4) == 4 &&
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move;
    }
    else if ((e.KeyState & 8) == 8 &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        // CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy;
    }
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }

    // Get the index of the item the mouse is below. 

    // The mouse locations are relative to the screen, so they must be 
    // converted to client coordinates.

    indexOfItemUnderMouseToDrop =
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y)));

    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
    {
        DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
    }
    else
    {
        DropLocationLabel.Text = "Drops at the end.";
    }
}

注解

此枚举由DragEventArgsGiveFeedbackEventArgsControl类使用。

可用于 DragDropEffects 显示用于拖放操作的不同鼠标指针。 例如,可以显示拖放操作的 Copy 加号、拖放操作的箭头符号 Move ,也可以显示带线条的红色圆圈,用于 None 拖放操作。

如果要在当前不可见的目标位置删除数据,则可以在拖动时滚动目标。 如果目标不支持滚动,则必须在开始拖放操作之前,确保放置位置在目标中可见。 下面是一些可能需要滚动目标的情况:

  • 正在将文本拖动到文档中,并且想要将文本放置在文档窗口中不可见的位置。

  • 正在将文件拖动到文件树中,并且想要将文件拖放到文件树中不可见的节点上。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅