Bagikan melalui


Control.DragOver Kejadian

Definisi

Terjadi ketika objek diseret ke atas batas kontrol.

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 

Jenis Acara

Contoh

Contoh kode berikut menunjukkan operasi seret dan letakkan antara dua kontrol ListBox. Contoh memanggil metode DoDragDrop saat tindakan seret dimulai. Tindakan seret dimulai jika mouse telah memindahkan lebih dari SystemInformation.DragSize dari lokasi mouse selama peristiwa MouseDown. Metode IndexFromPoint digunakan untuk menentukan indeks item yang akan diseret selama peristiwa MouseDown.

Contoh ini juga menunjukkan penggunaan kursor kustom untuk operasi seret dan letakkan. Contoh ini mengharuskan dua file kursor, 3dwarro.cur dan 3dwno.cur, ada di direktori aplikasi, untuk kursor seret dan tanpa jatuhkan kustom. Kursor kustom akan digunakan jika UseCustomCursorsCheckCheckBox dicentang. Kursor kustom diatur dalam penanganan aktivitas GiveFeedback.

Status keyboard dievaluasi di penanganan aktivitas DragOver untuk ListBoxkanan , untuk menentukan operasi seret apa yang akan didasarkan pada status tombol SHIFT, CTRL, ALT, atau CTRL+ALT. Lokasi di ListBox tempat penurunan akan terjadi juga ditentukan selama peristiwa DragOver. Jika data yang akan dihilangkan bukan String, maka DragEventArgs.Effect diatur ke None di DragDropEffects. Akhirnya, status penurunan ditampilkan di DropLocationLabelLabel.

Data yang akan dihilangkan untuk ListBox yang tepat ditentukan dalam penanganan aktivitas DragDrop dan nilai String ditambahkan di tempat yang sesuai di ListBox. Jika operasi seret bergerak di luar batas formulir, maka operasi seret dan letakkan dibatalkan di penanganan aktivitas QueryContinueDrag.

Kutipan kode ini menunjukkan menggunakan peristiwa DragOver. Lihat metode DoDragDrop untuk contoh kode lengkap.

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

Keterangan

Peristiwa DragOver dinaikkan ketika kursor mouse bergerak dalam batas kontrol selama operasi seret dan letakkan.

Berikut ini menjelaskan bagaimana dan kapan peristiwa yang terkait dengan operasi seret dan letakkan dinaikkan.

Metode DoDragDrop menentukan kontrol di bawah lokasi kursor saat ini. Kemudian memeriksa untuk melihat apakah kontrol adalah target penurunan yang valid.

Jika kontrol adalah target drop yang valid, peristiwa GiveFeedback dinaikkan dengan efek seret dan letakkan yang ditentukan. Untuk daftar efek seret dan letakkan, lihat enumerasi DragDropEffects.

Perubahan pada posisi kursor mouse, status keyboard, dan status tombol mouse dilacak.

  • Jika pengguna berpindah dari jendela, peristiwa DragLeave akan dinaikkan.

  • Jika mouse memasuki kontrol lain, DragEnter untuk kontrol tersebut akan dinaikkan.

  • Jika mouse bergerak tetapi tetap berada dalam kontrol yang sama, peristiwa DragOver dinaikkan.

Jika ada perubahan dalam status tombol keyboard atau mouse, peristiwa QueryContinueDrag dinaikkan dan menentukan apakah akan melanjutkan seret, untuk menghilangkan data, atau membatalkan operasi berdasarkan nilai properti Action dari QueryContinueDragEventArgsperistiwa .

  • Jika nilai DragActionContinue, peristiwa DragOver dinaikkan untuk melanjutkan operasi dan peristiwa GiveFeedback dinaikkan dengan efek baru sehingga umpan balik visual yang sesuai dapat diatur. Untuk daftar efek penurunan yang valid, lihat enumerasi DragDropEffects.

    Nota

    Peristiwa DragOver dan GiveFeedback dipasangkan sehingga saat mouse bergerak melintasi target drop, pengguna diberi umpan balik -tanggal yang paling up-topada posisi mouse.

  • Jika nilai DragActionDrop, nilai efek penurunan dikembalikan ke sumbernya, sehingga aplikasi sumber dapat melakukan operasi yang sesuai pada data sumber; misalnya, potong data jika operasi adalah pemindahan.

  • Jika nilai DragActionCancel, peristiwa DragLeave dinaikkan.

    Nota

    Properti X dan YDragEventArgs berada dalam koordinat layar, bukan koordinat klien. Baris kode C# berikut mengonversi properti menjadi klien Point:

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

Untuk informasi selengkapnya tentang menangani peristiwa, lihat Penanganan dan Penggalangan Peristiwa.

Berlaku untuk

Lihat juga