Condividi tramite


Control.DragOver Evento

Definizione

Si verifica quando un oggetto viene trascinato sui limiti del controllo.

public:
 event System::Windows::Forms::DragEventHandler ^ DragOver;
public event System.Windows.Forms.DragEventHandler DragOver;
public event System.Windows.Forms.DragEventHandler? DragOver;
member this.DragOver : System.Windows.Forms.DragEventHandler 
Public Custom Event DragOver As DragEventHandler 

Tipo evento

Esempio

Nell'esempio di codice seguente viene illustrata un'operazione di trascinamento della selezione tra due controlli ListBox. Nell'esempio viene chiamato il metodo DoDragDrop all'avvio dell'azione di trascinamento. L'azione di trascinamento inizia se il mouse è stato spostato più di SystemInformation.DragSize dalla posizione del mouse durante l'evento MouseDown. Il metodo IndexFromPoint viene utilizzato per determinare l'indice dell'elemento da trascinare durante l'evento MouseDown.

L'esempio illustra anche l'uso di cursori personalizzati per l'operazione di trascinamento della selezione. L'esempio richiede che due file di cursore, 3dwarro.cur e 3dwno.cur, esistano nella directory dell'applicazione, rispettivamente per il trascinamento personalizzato e i cursori senza rilascio. I cursori personalizzati verranno utilizzati se è selezionata la UseCustomCursorsCheckCheckBox. I cursori personalizzati vengono impostati nel gestore eventi GiveFeedback.

Lo stato della tastiera viene valutato nel gestore eventi DragOver per il ListBoxdestro, per determinare quale operazione di trascinamento si baserà sullo stato dei tasti MAIUSC, CTRL, ALT o CTRL+ALT. La posizione nel ListBox in cui si verifica l'eliminazione viene determinata anche durante l'evento DragOver. Se i dati da eliminare non sono un String, il DragEventArgs.Effect è impostato su None in DragDropEffects. Infine, lo stato del rilascio viene visualizzato nel DropLocationLabelLabel.

I dati da eliminare per il ListBox corretto sono determinati nel gestore eventi DragDrop e il valore String viene aggiunto nella posizione appropriata nel ListBox. Se l'operazione di trascinamento si sposta all'esterno dei limiti del form, l'operazione di trascinamento della selezione viene annullata nel gestore eventi QueryContinueDrag.

Questo estratto di codice illustra l'uso dell'evento DragOver. Vedere il metodo DoDragDrop per l'esempio di codice completo.

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

Commenti

L'evento DragOver viene generato quando il cursore del mouse viene spostato all'interno dei limiti del controllo durante un'operazione di trascinamento della selezione.

Di seguito viene descritto come e quando vengono generati eventi correlati alle operazioni di trascinamento della selezione.

Il metodo DoDragDrop determina il controllo sotto la posizione corrente del cursore. Controlla quindi se il controllo è una destinazione di rilascio valida.

Se il controllo è una destinazione di rilascio valida, l'evento GiveFeedback viene generato con l'effetto di trascinamento della selezione specificato. Per un elenco degli effetti di trascinamento della selezione, vedere l'enumerazione DragDropEffects.

Vengono rilevate modifiche alla posizione del cursore del mouse, allo stato della tastiera e al pulsante del mouse.

  • Se l'utente si sposta all'esterno di una finestra, viene generato l'evento DragLeave.

  • Se il mouse entra in un altro controllo, viene generato il DragEnter per tale controllo.

  • Se il mouse si sposta ma rimane all'interno dello stesso controllo, viene generato l'evento DragOver.

Se si verifica una modifica dello stato della tastiera o del pulsante del mouse, viene generato l'evento QueryContinueDrag e determina se continuare il trascinamento, rilasciare i dati o annullare l'operazione in base al valore della proprietà Action dell'evento QueryContinueDragEventArgs.

  • Se il valore di DragAction è Continue, viene generato l'evento DragOver per continuare l'operazione e l'evento GiveFeedback viene generato con il nuovo effetto in modo che sia possibile impostare il feedback visivo appropriato. Per un elenco di effetti di rilascio validi, vedere l'enumerazione DragDropEffects.

    Nota

    Gli eventi DragOver e GiveFeedback vengono associati in modo che quando il mouse si sposta attraverso la destinazione di rilascio, all'utente viene assegnato il feedback più up-to-date sulla posizione del mouse.

  • Se il valore di DragAction è Drop, il valore dell'effetto di rilascio viene restituito all'origine, in modo che l'applicazione di origine possa eseguire l'operazione appropriata sui dati di origine; Ad esempio, tagliare i dati se l'operazione è stata spostata.

  • Se il valore di DragAction è Cancel, viene generato l'evento DragLeave.

    Nota

    Le proprietà X e Y del DragEventArgs si trovano nelle coordinate dello schermo, non nelle coordinate client. La riga di codice C# seguente converte le proprietà in un client Point:

    Point clientPoint = targetControl.PointToClient(new Point(de. X, de. Y));

Per altre informazioni sulla gestione degli eventi, vedere Gestione e generazione di eventi.

Si applica a

Vedi anche