Aracılığıyla paylaş


DragEventArgs Sınıf

Tanım

DragDrop, DragEnterveya DragOver olayı için veri sağlar.

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
Devralma
DragEventArgs
Türetilmiş
Öznitelikler

Örnekler

Aşağıdaki örnekte, iki ListBox denetimi arasındaki sürükle ve bırak işlemi gösterilmektedir. Örnek, sürükleme eylemi başladığında DoDragDrop yöntemini çağırır. Sürükleme eylemi, MouseDown olayı sırasında farenin fare konumundan SystemInformation.DragSize fazla hareket ettiyse başlar. IndexFromPoint yöntemi, MouseDown olayı sırasında sürüklenecek öğenin dizinini belirlemek için kullanılır.

Örnekte, sürükle ve bırak işlemi için özel imleçlerin kullanılması da gösterilmektedir. Örnek, özel sürükleme ve bırakmama imleçleri için uygulama dizininde sırasıyla 3dwarro.cur ve 3dwno.curiki imleç dosyasının bulunduğunu varsayar. UseCustomCursorsCheck CheckBox işaretliyse özel imleçler kullanılır. Özel imleçler GiveFeedback olay işleyicisinde ayarlanır.

Klavye durumu, shift, CTRL, ALT veya CTRL+ALT tuşlarının durumuna bağlı olarak sürükleme işleminin ne olacağını belirlemek üzere doğru ListBoxiçin DragOver olay işleyicisinde değerlendirilir. ListBox bırakmanın gerçekleşeceği konum da DragOver olayı sırasında belirlenir. Bırakacak veriler bir Stringdeğilse, DragEventArgs.EffectDragDropEffects.Noneolarak ayarlanır. Son olarak, bırakmanın durumu DropLocationLabelLabelgörüntülenir.

Doğru ListBox bırakacak veriler DragDrop olay işleyicisinde belirlenir ve String değeri ListBoxuygun yere eklenir. Sürükleme işlemi formun sınırlarının dışına taşınırsa, QueryContinueDrag olay işleyicisinde sürükle ve bırak işlemi iptal edilir.

Bu kod alıntısı, DragEventArgs sınıfını kullanmayı gösterir. Kod örneğinin tamamı için DoDragDrop yöntemine bakın.

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

Aşağıdaki örnekte, sürükle ve bırak işleminin kaynağı ve hedefi arasında DragEventArgs nasıl geçirildiğini gösterilmektedir. Bu örnekte ListBox denetimi verilerin kaynağı, RichTextBox denetimi ise hedeftir. Örnekte, ListBox denetiminin geçerli dosya adlarının listesiyle doldurulduğunu varsayar. Kullanıcı görüntülenen dosya adlarından birini ListBox denetiminden RichTextBox denetimine sürüklediğinde, dosya adında başvuruda bulunılan dosya açılır.

İşlem, ListBox denetiminin MouseDown olayında başlatılır. DragEnter olay işleyicisinde örnek, verilerin RichTextBox denetiminin görüntüleyebileceği bir biçimde olduğunu doğrulamak için GetDataPresent yöntemini kullanır ve ardından verilerin kaynak denetimden hedef denetime kopyalanması gerektiğini belirtmek için DragDropEffects özelliğini ayarlar. Son olarak, RichTextBox denetiminin DragDrop olay işleyicisi açmak üzere dosya adını almak için GetData yöntemini kullanır.

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

Açıklamalar

DragDrop olayı, kullanıcı bir nesneyi denetimin üzerine sürükleyip fare düğmesini serbest bırakarak denetime bırakarak sürükleyip bırakma işlemini tamamladığında gerçekleşir. DragEnter olayı, kullanıcı fareyle bir nesneyi sürüklerken fare işaretçisini denetime taşırken gerçekleşir. DragOver olayı, kullanıcı fareyle bir nesneyi sürüklerken fare işaretçisini denetimin üzerine getirince gerçekleşir.

DragEventArgs nesnesi bu olayla ilişkili tüm verileri belirtir; SHIFT, CTRL ve ALT tuşlarının geçerli durumu; fare işaretçisinin konumu; ve sürükleme olayının kaynağı ve hedefi tarafından izin verilen sürükle ve bırak efektleri.

Olay modeli hakkında bilgi için bkz. İşleme ve Olayları Oluşturma.

Oluşturucular

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

DragEventArgs sınıfının yeni bir örneğini başlatır.

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

DragEventArgs sınıfının yeni bir örneğini başlatır.

Özellikler

AllowedEffect

Sürükleme olayının kaynağı (veya kaynağı) tarafından hangi sürükle ve bırak işlemlerine izin verilir alır.

Data

Bu olayla ilişkili verileri içeren IDataObject alır.

DropImageType

Bırakma açıklaması görüntü türünü alır veya ayarlar.

Effect

Sürükle ve bırak işleminde hedef bırakma efektini alır veya ayarlar.

KeyState

SHIFT, CTRL ve ALT tuşlarının geçerli durumunun yanı sıra fare düğmelerinin durumunu alır.

Message

"%1taşı" gibi bırakma açıklaması metnini alır veya ayarlar.

MessageReplacementToken

Messageiçinde %1 belirtildiğinde , "Belgeler" gibi bırakma açıklaması metnini alır veya ayarlar.

X

Fare işaretçisinin x koordinatını ekran koordinatlarında alır.

Y

Fare işaretçisinin y koordinatını ekran koordinatlarında alır.

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Geçerli örneğin Type alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectbasit bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli nesneyi temsil eden bir dize döndürür.

(Devralındığı yer: Object)

Şunlara uygulanır

Ayrıca bkz.