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 ListBox controlli. Nell'esempio viene chiamato il DoDragDrop metodo all'avvio dell'azione di trascinamento. L'azione di trascinamento inizia se il mouse è stato spostato più che SystemInformation.DragSize dalla posizione del mouse durante l'evento MouseDown . Il IndexFromPoint metodo viene usato 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. L'esempio richiede che due file 3dwarro.cur di cursore e 3dwno.cur, esistano rispettivamente nella directory dell'applicazione, per il trascinamento personalizzato e i cursori senza rilascio. I cursori personalizzati verranno usati se viene UseCustomCursorsCheckCheckBox controllato. I cursori personalizzati vengono impostati nel GiveFeedback gestore eventi.

Lo stato della tastiera viene valutato nel DragOver gestore eventi per il diritto ListBox, per determinare quale operazione di trascinamento sarà in base allo stato dei tasti MAIUSC, CTRL, ALT o CTRL+ALT. La posizione in ListBox cui si verificherebbe l'eliminazione viene determinata anche durante l'evento DragOver . Se i dati da eliminare non sono un Stringoggetto , l'oggetto DragEventArgs.Effect è impostato su None in DragDropEffects. Infine, lo stato dell'eliminazione DropLocationLabelLabelviene visualizzato in .

I dati da eliminare per il diritto ListBox sono determinati nel DragDrop gestore eventi e il String valore viene aggiunto al posto appropriato in ListBox. Se l'operazione di trascinamento si sposta all'esterno dei limiti del modulo, l'operazione di trascinamento viene annullata nel QueryContinueDrag gestore eventi.

Questo estratto di codice illustra l'uso dell'evento DragOver . Vedere il metodo per l'esempio DoDragDrop 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 si sposta all'interno dei limiti del controllo durante un'operazione di trascinamento.

Di seguito vengono illustrate le modalità e le circostanze in cui vengono generati eventi relativi a operazioni di trascinamento.

Il DoDragDrop metodo determina il controllo nella posizione del cursore corrente. Verifica 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 trascinamento specificato. Per un elenco degli effetti di trascinamento e rilascio, vedere l'enumerazione DragDropEffects.

Viene tenuta traccia delle modifiche apportate alla posizione del cursore del mouse, allo stato della tastiera e allo stato dei pulsanti del mouse.

  • Se l'utente si sposta al di fuori di una finestra, verrà generato un evento DragLeave.

  • Se il mouse viene spostato su un altro controllo, verrà generato l'evento DragEnter per tale controllo.

  • Se il mouse viene spostato ma sempre all'interno dello stesso controllo, verrà generato l'evento DragOver.

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

  • Se il valore è DragActionContinue, l'evento DragOver viene generato per continuare l'operazione e l'evento GiveFeedback viene generato con il nuovo effetto in modo che sia possibile impostare commenti visivi appropriati. Per un elenco degli effetti di trascinamento validi, vedere l'enumerazione DragDropEffects.

    Nota

    Gli DragOver eventi e GiveFeedback vengono associati in modo che, quando il mouse si sposta attraverso la destinazione di rilascio, l'utente riceve il feedback più aggiornato sulla posizione del mouse.

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

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

    Nota

    Le X proprietà e Y dell'oggetto DragEventArgs sono in coordinate dello schermo, non coordinate client. La riga di codice C# seguente converte le proprietà in un client Point:

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

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.

Si applica a

Vedi anche