DragEventArgs Klasa

Definicja

Dostarcza dane dla DragDropzdarzenia , DragEnterlub 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
Dziedziczenie
DragEventArgs
Pochodne
Atrybuty

Przykłady

W poniższym przykładzie pokazano operację przeciągania i upuszczania między dwoma ListBox kontrolkami. Przykład wywołuje metodę DoDragDrop po uruchomieniu akcji przeciągania. Akcja przeciągania rozpoczyna się, jeśli mysz przeniosła się więcej niż SystemInformation.DragSize z lokalizacji myszy podczas MouseDown zdarzenia. Metoda IndexFromPoint służy do określania indeksu elementu do przeciągania podczas MouseDown zdarzenia.

W przykładzie pokazano również użycie niestandardowych kursorów dla operacji przeciągania i upuszczania. W przykładzie przyjęto założenie, że dwa pliki 3dwarro.cur kursorów i 3dwno.cur, istnieją w katalogu aplikacji, odpowiednio dla niestandardowego przeciągania i bez upuszczania kursorów. Kursory niestandardowe będą używane, jeśli element jest zaznaczony UseCustomCursorsCheckCheckBox . Kursory niestandardowe są ustawiane w procedurze obsługi zdarzeń GiveFeedback .

Stan klawiatury jest oceniany w procedurze DragOver obsługi zdarzeń po prawej stronie ListBox, aby określić, co operacja przeciągania będzie oparta na stanie klawiszy SHIFT, CTRL, ALT lub CTRL+ALT. Lokalizacja, w ListBox której występuje spadek, jest również określana podczas DragOver zdarzenia. Jeśli dane do porzucania nie są wartością String, DragEventArgs.Effect parametr jest ustawiony na DragDropEffects.Nonewartość . Na koniec stan upuszczania jest wyświetlany w elemecie DropLocationLabelLabel.

Dane do porzucania po prawej stronie ListBox są określane w procedurze DragDrop obsługi zdarzeń, a String wartość jest dodawana w odpowiednim miejscu w ListBoxobiekcie . Jeśli operacja przeciągania przesuwa się poza granice formularza, operacja przeciągania i upuszczania zostanie anulowana w procedurze QueryContinueDrag obsługi zdarzeń.

Ten fragment kodu przedstawia użycie DragEventArgs klasy . Zobacz metodę DoDragDrop kompletnego przykładu kodu.

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

W poniższym przykładzie pokazano, jak DragEventArgs są przekazywane między źródłem i obiektem docelowym operacji przeciągania i upuszczania. W tym przykładzie kontrolka ListBox jest źródłem danych, a kontrolka RichTextBox jest obiektem docelowym. W przykładzie przyjęto założenie, że kontrolka ListBox została wypełniona listą prawidłowych nazw plików. Gdy użytkownik przeciągnie jedną z wyświetlanych nazw plików z ListBox kontrolki do kontrolki RichTextBox , zostanie otwarty plik, do którego odwołuje się nazwa pliku.

Operacja jest inicjowana w ListBox zdarzeniu MouseDown kontrolki. W procedurze DragEnter obsługi zdarzeń przykład używa GetDataPresent metody w celu sprawdzenia, czy dane są w formacie, który RichTextBox może być wyświetlany, a następnie ustawia DragDropEffects właściwość w celu określenia, że dane powinny być kopiowane z kontrolki źródła do kontrolki docelowej. RichTextBox Na koniec procedura obsługi zdarzeń DragDrop kontrolki używa GetData metody w celu pobrania nazwy pliku do otwarcia.

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

Uwagi

Zdarzenie DragDrop występuje, gdy użytkownik ukończy operację przeciągania i upuszczania, przeciągając obiekt nad kontrolką, a następnie upuszczając go do kontrolki, zwalniając przycisk myszy. Zdarzenie DragEnter występuje, gdy użytkownik przesuwa wskaźnik myszy na kontrolkę podczas przeciągania obiektu myszą. Zdarzenie DragOver występuje, gdy użytkownik przesuwa wskaźnik myszy nad kontrolką podczas przeciągania obiektu myszą.

DragEventArgs Obiekt określa wszystkie dane skojarzone z tym zdarzeniem; bieżący stan klawiszy SHIFT, CTRL i ALT; lokalizację wskaźnika myszy oraz efekty przeciągania i upuszczania dozwolone przez źródło i cel zdarzenia przeciągania.

Aby uzyskać informacje o modelu zdarzeń, zobacz Obsługa i zgłaszanie zdarzeń.

Konstruktory

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

Inicjuje nowe wystąpienie klasy DragEventArgs.

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

Inicjuje nowe wystąpienie klasy DragEventArgs.

Właściwości

AllowedEffect

Pobiera operacje przeciągania i upuszczania dozwolone przez obiekt źródłowy (lub źródło) zdarzenia przeciągania.

Data

Pobiera element IDataObject zawierający dane skojarzone z tym zdarzeniem.

DropImageType

Pobiera lub ustawia typ obrazu opisu upuszczania.

Effect

Pobiera lub ustawia efekt upuszczania docelowego w operacji przeciągania i upuszczania.

KeyState

Pobiera bieżący stan klawiszy SHIFT, CTRL i ALT, a także stan przycisków myszy.

Message

Pobiera lub ustawia tekst opisu upuszczania, taki jak "Przenieś do %1".

MessageReplacementToken

Pobiera lub ustawia tekst opisu upuszczania, taki jak "Dokumenty", gdy %1 jest określony w .Message

X

Pobiera współrzędną x wskaźnika myszy we współrzędnych ekranu.

Y

Pobiera współrzędną y wskaźnika myszy we współrzędnych ekranu.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też