DragEventHandler Delegat

Definition

Stellt die Methode dar, die das Ereignis DragDrop, DragEnter oder DragOver von Control behandelt.

public delegate void DragEventHandler(System::Object ^ sender, DragEventArgs ^ e);
public delegate void DragEventHandler(object sender, DragEventArgs e);
public delegate void DragEventHandler(object? sender, DragEventArgs e);
type DragEventHandler = delegate of obj * DragEventArgs -> unit
Public Delegate Sub DragEventHandler(sender As Object, e As DragEventArgs)

Parameter

sender
Object

Die Quelle des Ereignisses.

e
DragEventArgs

Ein DragEventArgs, das die Ereignisdaten enthält.

Beispiele

Im folgenden Beispiel wird ein Drag-and-Drop-Vorgang zwischen zwei ListBox Steuerelementen veranschaulicht. Im Beispiel wird die DoDragDrop -Methode aufgerufen, wenn die Ziehaktion gestartet wird. Die Ziehaktion wird gestartet, wenn sich die Maus während MouseDown des Ereignisses mehr als SystemInformation.DragSize von der Mausposition bewegt hat. Die IndexFromPoint -Methode wird verwendet, um den Index des Elements zu bestimmen, das während des Ereignisses MouseDown gezogen werden soll.

Das Beispiel veranschaulicht auch die Verwendung benutzerdefinierter Cursor für den Drag-and-Drop-Vorgang. Im Beispiel wird davon ausgegangen, dass zwei Cursordateien und 3dwarro.cur3dwno.curim Anwendungsverzeichnis für die benutzerdefinierten Cursor drag und no drop vorhanden sind. Die benutzerdefinierten Cursor werden verwendet, wenn die UseCustomCursorsCheckCheckBox aktiviert ist. Die benutzerdefinierten Cursor werden im GiveFeedback Ereignishandler festgelegt.

Der Tastaturzustand wird im DragOver Ereignishandler für die rechte ListBoxausgewertet, um zu bestimmen, welcher Ziehvorgang auf der Grundlage des Zustands der UMSCHALT-, STRG-, ALT- oder STRG+ALT-TASTEN erfolgt. Die Position in der, an der ListBox der Abbruch erfolgen würde, wird auch während des Ereignisses DragOver bestimmt. Wenn die zu löschenden Daten keine Stringsind, wird auf DragEventArgs.EffectDragDropEffects.Nonefestgelegt. Schließlich wird die status des Drops im DropLocationLabelLabelangezeigt.

Die für die rechte ListBox Seite zu löschenden Daten werden im DragDrop -Ereignishandler bestimmt, und der String Wert wird an der entsprechenden Stelle im ListBoxhinzugefügt. Wenn der Ziehvorgang außerhalb der Grenzen des Formulars verschoben wird, wird der Drag-and-Drop-Vorgang im QueryContinueDrag Ereignishandler abgebrochen.

Dieser Codeauszug veranschaulicht die Verwendung des DragEventHandler Delegaten mit dem DragOver Ereignis. DoDragDrop Das vollständige Codebeispiel finden Sie in der -Methode.

void ListDragTarget_DragOver( Object^ /*sender*/, System::Windows::Forms::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( System::String::typeid ) )
   {
      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( Point(e->X,e->Y) ) );
   
   // Updates the label text.
   if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches )
   {
      DropLocationLabel->Text = String::Concat( "Drops before item # ", (indexOfItemUnderMouseToDrop + 1) );
   }
   else
            DropLocationLabel->Text = "Drops at the end.";
}
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.";
    }
}
Private Sub ListDragTarget_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragOver
    ' Determine whether string data exists in the drop data. If not, then
    ' the drop effect reflects that the drop cannot occur.
    If Not (e.Data.GetDataPresent(GetType(System.String))) Then

        e.Effect = DragDropEffects.None
        DropLocationLabel.Text = "None - no string data."
        Return
    End If

    ' Set the effect based upon the KeyState.
    If ((e.KeyState And (8 + 32)) = (8 + 32) And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then
        ' KeyState 8 + 32 = CTRL + ALT

        ' Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 32) = 32 And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then

        ' ALT KeyState for link.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 4) = 4 And
        (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move

    ElseIf ((e.KeyState And 8) = 8 And
        (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then

        ' CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy

    ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move

    Else
        e.Effect = DragDropEffects.None
    End If

    ' Gets 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) Then
        DropLocationLabel.Text = "Drops before item #" & (indexOfItemUnderMouseToDrop + 1)
    Else
        DropLocationLabel.Text = "Drops at the end."
    End If

End Sub

Hinweise

Beim Erstellen eines DragEventHandler-Delegaten bestimmen Sie die Methode für die Ereignisbehandlung. Um dem Ereignishandler das Ereignis zuzuordnen, fügen Sie dem Ereignis eine Instanz des Delegaten hinzu. Der Ereignishandler wird bei jedem Eintreten des Ereignisses aufgerufen, sofern der Delegat nicht entfernt wird. Weitere Informationen zum Behandeln von Ereignissen mit Delegaten finden Sie unter Behandeln und Auslösen von Ereignissen.

Erweiterungsmethoden

GetMethodInfo(Delegate)

Ruft ein Objekt ab, das die Methode darstellt, die vom angegebenen Delegaten dargestellt wird.

Gilt für:

Weitere Informationen