DragEventArgs Classe

Definizione

Fornisce i dati per l'evento DragDrop, DragEnter o DragOver.

public ref class DragEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class DragEventArgs : EventArgs
public class DragEventArgs : EventArgs
[<System.Runtime.InteropServices.ComVisible(true)>]
type DragEventArgs = class
    inherit EventArgs
type DragEventArgs = class
    inherit EventArgs
Public Class DragEventArgs
Inherits EventArgs
Ereditarietà
DragEventArgs
Derivato
Attributi

Esempio

Nell'esempio 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. Nell'esempio si presuppone 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à basata sullo 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 , Stringl'oggetto DragEventArgs.Effect è impostato su DragDropEffects.None. 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 della DragEventArgs classe . 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

Nell'esempio seguente viene illustrato come DragEventArgs vengono passati tra l'origine e la destinazione di un'operazione di trascinamento. In questo esempio un ListBox controllo è l'origine dei dati e il RichTextBox controllo è la destinazione. L'esempio presuppone che il ListBox controllo sia stato popolato con un elenco di nomi file validi. Quando l'utente trascina uno dei file visualizzati dal ListBox controllo nel RichTextBox controllo, viene aperto il file a cui fa riferimento nel nome file.

L'operazione viene avviata nell'evento ListBox MouseDown del controllo. DragEnter Nel gestore eventi l'esempio usa il GetDataPresent metodo per verificare che i dati siano in un formato che il RichTextBox controllo può visualizzare e quindi imposta la DragDropEffects proprietà per specificare che i dati devono essere copiati dal controllo del codice sorgente al controllo di destinazione. Infine, il RichTextBox gestore eventi DragDrop del controllo usa il metodo per recuperare il GetData nome file da aprire.

private:
   void Form1_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      // Sets the AllowDrop property so that data can be dragged onto the control.
      richTextBox1->AllowDrop = true;

      // Add code here to populate the ListBox1 with paths to text files.
   }

   void listBox1_MouseDown( Object^ sender, System::Windows::Forms::MouseEventArgs^ e )
   {
      // Determines which item was selected.
      ListBox^ lb = (dynamic_cast<ListBox^>(sender));
      Point pt = Point(e->X,e->Y);
      int index = lb->IndexFromPoint( pt );

      // Starts a drag-and-drop operation with that item.
      if ( index >= 0 )
      {
         lb->DoDragDrop( lb->Items[ index ], DragDropEffects::Link );
      }
   }

   void richTextBox1_DragEnter( Object^ /*sender*/, DragEventArgs^ e )
   {
      // If the data is text, copy the data to the RichTextBox control.
      if ( e->Data->GetDataPresent( "Text" ) )
            e->Effect = DragDropEffects::Copy;
   }

   void richTextBox1_DragDrop( Object^ /*sender*/, DragEventArgs^ e )
   {
      // Loads the file into the control.
      richTextBox1->LoadFile( dynamic_cast<String^>(e->Data->GetData( "Text" )), System::Windows::Forms::RichTextBoxStreamType::RichText );
   }
private void Form1_Load(object sender, EventArgs e) 
{
   // Sets the AllowDrop property so that data can be dragged onto the control.
   richTextBox1.AllowDrop = true;

   // Add code here to populate the ListBox1 with paths to text files.
}

private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   // Determines which item was selected.
   ListBox lb =( (ListBox)sender);
   Point pt = new Point(e.X,e.Y);
   int index = lb.IndexFromPoint(pt);

   // Starts a drag-and-drop operation with that item.
   if(index>=0) 
   {
      lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link);
   }
}

private void richTextBox1_DragEnter(object sender, DragEventArgs e) 
{
   // If the data is text, copy the data to the RichTextBox control.
   if(e.Data.GetDataPresent("Text"))
      e.Effect = DragDropEffects.Copy;
}

private void richTextBox1_DragDrop(object sender, DragEventArgs e) 
{
   // Loads the file into the control. 
   richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   ' Sets the AllowDrop property so that data can be dragged onto the control.
   RichTextBox1.AllowDrop = True

   ' Add code here to populate the ListBox1 with paths to text files.

End Sub

Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
   ' If the data is text, copy the data to the RichTextBox control.
   If (e.Data.GetDataPresent("Text")) Then
      e.Effect = DragDropEffects.Copy
   End If
End Sub


Private Overloads Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
   ' Loads the file into the control. 
   RichTextBox1.LoadFile(e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText)
End Sub

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
   Dim Lb As ListBox
   Dim Pt As New Point(e.X, e.Y)
   Dim Index As Integer

   ' Determines which item was selected.
   Lb = sender
   Index = Lb.IndexFromPoint(Pt)

   ' Starts a drag-and-drop operation with that item.
   If Index >= 0 Then
      Lb.DoDragDrop(Lb.Items(Index), DragDropEffects.Link)
   End If
End Sub

Commenti

L'evento DragDrop si verifica quando l'utente completa un'operazione di trascinamento trascinando un oggetto sul controllo e quindi rilasciando il pulsante del mouse. L'evento DragEnter si verifica quando l'utente sposta il puntatore del mouse sul controllo durante il trascinamento di un oggetto con il mouse. L'evento DragOver si verifica quando l'utente sposta il puntatore del mouse sul controllo durante il trascinamento di un oggetto con il mouse.

Un DragEventArgs oggetto specifica tutti i dati associati a questo evento, lo stato corrente dei tasti MAIUSC, CTRL e ALT, la posizione del puntatore del mouse e gli effetti di trascinamento consentiti dall'origine e dalla destinazione dell'evento di trascinamento.

Per informazioni sul modello di evento, vedere Gestione e generazione di eventi.

Costruttori

DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects)

Inizializza una nuova istanza della classe DragEventArgs.

DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects, DropImageType, String, String)

Inizializza una nuova istanza della classe DragEventArgs.

Proprietà

AllowedEffect

Ottiene le operazioni di trascinamento consentite dall'elemento usato per generare l'evento di trascinamento.

Data

Ottiene l'oggetto IDataObject che contiene i dati associati a questo evento.

DropImageType

Ottiene o imposta il tipo di immagine della descrizione di rilascio.

Effect

Ottiene o imposta l'effetto di rilascio di destinazione in un'operazione di trascinamento.

KeyState

Ottiene lo stato corrente dei tasti MAIUSC, CTRL e ALT, nonché lo stato dei pulsanti del mouse.

Message

Ottiene o imposta il testo della descrizione di eliminazione, ad esempio "Sposta su %1".

MessageReplacementToken

Ottiene o imposta il testo della descrizione di eliminazione, ad esempio "Documenti", quando viene specificato %1 in Message.

X

Ottiene la coordinata x del puntatore del mouse espressa in coordinate dello schermo.

Y

Ottiene la coordinata y del puntatore del mouse espressa in coordinate dello schermo.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche