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.";
    }
}

備註

GiveFeedbackEventArgsControl 類別會使用此 DragEventArgs 列舉。

您可以使用 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

另請參閱