Share via


Control.DragOver Kejadian

Definisi

Terjadi ketika objek diseret di 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 di antara dua ListBox kontrol. Contoh memanggil DoDragDrop metode saat tindakan seret dimulai. Tindakan seret dimulai jika mouse telah memindahkan lebih dari SystemInformation.DragSize dari lokasi mouse selama MouseDown peristiwa. Metode IndexFromPoint ini digunakan untuk menentukan indeks item yang akan diseret selama MouseDown peristiwa.

Contoh ini juga menunjukkan penggunaan kursor kustom untuk operasi seret dan letakkan. Contohnya 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 GiveFeedback aktivitas.

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

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

Kutipan kode ini menunjukkan penggunaan peristiwa.DragOver DoDragDrop Lihat metode 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 saat kursor mouse bergerak di 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 penurunan yang valid, GiveFeedback peristiwa dinaikkan dengan efek seret dan letakkan yang ditentukan. Untuk daftar efek seret dan letakkan, lihat DragDropEffects enumerasi.

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

  • Jika pengguna bergerak keluar dari jendela, DragLeave peristiwa akan dinaikkan.

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

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

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

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

    Catatan

    Peristiwa DragOver dan GiveFeedback dipasangkan sehingga saat mouse bergerak melintasi target penurunan, pengguna diberi umpan balik terbaru tentang posisi mouse.

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

  • Jika nilai DragAction adalah Cancel, DragLeave peristiwa dinaikkan.

    Catatan

    Properti XDragEventArgs dan Y berada di 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 Menangani dan Menaikkan Peristiwa.

Berlaku untuk

Lihat juga