Freigeben über


DragEventHandler-Delegat

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Delegate Sub DragEventHandler ( _
    sender As Object, _
    e As DragEventArgs _
)
'Usage
Dim instance As New DragEventHandler(AddressOf HandlerMethod)
public delegate void DragEventHandler (
    Object sender,
    DragEventArgs e
)
public delegate void DragEventHandler (
    Object^ sender, 
    DragEventArgs^ e
)
/** @delegate */
public delegate void DragEventHandler (
    Object sender, 
    DragEventArgs e
)
JScript unterstützt die Verwendung von Delegaten, aber nicht die Deklaration von neuen Delegaten.

Parameter

  • sender
    Die Quelle des Ereignisses.

Hinweise

Beim Erstellen eines DragEventHandler-Delegaten bestimmen Sie die Methode für die Ereignisbehandlung. Um das Ereignis mit dem Ereignishandler zu verknüpfen, 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 über die Ereignisbehandlung mit Delegaten finden Sie unter Ereignisse und Delegaten.

Beispiel

Im folgenden Beispiel wird ein Drag & Drop-Vorgang zwischen zwei ListBox-Steuerelementen veranschaulicht. In diesem Beispiel wird die DoDragDrop-Methode aufgerufen, wenn der Ziehvorgang begonnen wird. Der Ziehvorgang beginnt, wenn die Maus um mehr als SystemInformation.DragSize von der Mausposition während des MouseDown-Ereignisses verschoben wurde. Mit der IndexFromPoint-Methode kann der Index des im MouseDown-Ereignis zu ziehenden Elements bestimmt werden.

Im Beispiel wird außerdem die Verwendung benutzerdefinierter Cursor bei einem Drag & Drop-Vorgang veranschaulicht. Im Beispiel wird davon ausgegangen, dass die beiden Cursordateien 3dwarro.cur und 3dwno.cur für den benutzerdefinierten Ziehcursor bzw. den Cursor, der angezeigt wird, wenn ein Ablegen nicht möglich ist, im Anwendungsverzeichnis vorhanden sind. Die benutzerdefinierten Cursor werden verwendet, wenn die UseCustomCursorsCheckCheckBox aktiviert wurde. Die benutzerdefinierten Cursor werden im GiveFeedback-Ereignishandler festgelegt.

Der Tastaturzustand wird vom DragOver-Ereignishandler für die rechte ListBox ausgewertet, um je nach Zustand der UMSCHALTTASTE oder von STRG, ALT bzw. STRG+ALT den auszuführenden Ziehvorgang zu ermitteln. Außerdem wird beim DragOver-Ereignis auch die Position in der ListBox bestimmt, für die der Ablegevorgang ausgeführt wird. Wenn die abzulegenden Daten kein String sind, wird DragEventArgs.Effect auf DragDropEffects.None festgelegt. Abschließend wird der Status des Ablegevorgangs im DropLocationLabelLabel angezeigt.

Die in der rechten ListBox abzulegenden Daten werden im DragDrop-Ereignishandler bestimmt. Der String-Wert wird an der entsprechenden Stelle in die ListBox eingefügt. Wenn der Ziehvorgang außerhalb der Grenzen des Formulars beendet wird, wird der Drag & Drop-Vorgang im QueryContinueDrag-Ereignishandler abgebrochen.

In diesem Code wird die Verwendung des DragEventHandler-Delegaten mit dem DragOver-Ereignis veranschaulicht. Das vollständige Codebeispiel finden Sie unter der DoDragDrop-Methode.

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 = CTL + 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

        ' CTL 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
private 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(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 = CTL + 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) {

        // CTL 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.";

}
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 = CTL + 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) )
   {
      // CTL 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, 
    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.get_Data().GetDataPresent(String.class.ToType()))) {
        e.set_Effect(DragDropEffects.None);
        dropLocationLabel.set_Text("None - no string data.");
        return;
    }
    // Set the effect based upon the KeyState.
    if ((e.get_KeyState() & 8 + 32) == 8 + 32 && (e.get_AllowedEffect() 
        & DragDropEffects.Link) == DragDropEffects.Link) {
        // KeyState 8 + 32 = CTL + ALT
        // Link drag-and-drop effect.
        e.set_Effect(DragDropEffects.Link);
    }
    else {
        if ((e.get_KeyState() & 32) == 32 && (e.get_AllowedEffect() 
            & DragDropEffects.Link) == DragDropEffects.Link) {
            // ALT KeyState for link.
            e.set_Effect(DragDropEffects.Link);
        }
        else {
            if ((e.get_KeyState() & 4) == 4 && (e.get_AllowedEffect() 
                & DragDropEffects.Move) == DragDropEffects.Move) {
                // SHIFT KeyState for move.
                e.set_Effect(DragDropEffects.Move);
            }
            else {
                if ((e.get_KeyState() & 8) == 8 && (e.get_AllowedEffect() 
                    & DragDropEffects.Copy) == DragDropEffects.Copy) {
                    // CTL KeyState for copy.
                    e.set_Effect(DragDropEffects.Copy);
                }
                else {
                    if ((e.get_AllowedEffect() & DragDropEffects.Move) 
                        == DragDropEffects.Move) {
                        // By default, the drop action should be move, 
                        //if allowed.
                        e.set_Effect(DragDropEffects.Move);
                    }
                    else {
                        e.set_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.get_X(), e.get_Y())));
    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) {
        dropLocationLabel.set_Text("Drops before item #" 
            + (indexOfItemUnderMouseToDrop + 1));
    }
    else {
        dropLocationLabel.set_Text("Drops at the end.");
    }
} //listDragTarget_DragOver

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

System.Windows.Forms-Namespace
OnDragDrop
Control.DragDrop-Ereignis
OnDragEnter
Control.DragEnter-Ereignis
OnDragOver
Control.DragOver-Ereignis
DragEventArgs-Klasse