ListViewInsertionMark.NearestIndex(Point) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengambil indeks item yang paling dekat dengan titik yang ditentukan.
public:
int NearestIndex(System::Drawing::Point pt);
public int NearestIndex (System.Drawing.Point pt);
member this.NearestIndex : System.Drawing.Point -> int
Public Function NearestIndex (pt As Point) As Integer
Parameter
Mengembalikan
Indeks item yang paling dekat dengan titik yang ditentukan atau -1 jika item terdekat adalah item yang saat ini sedang diseret.
Contoh
Contoh kode berikut menunjukkan cara menggunakan ListView fitur tanda penyisipan dan mengimplementasikan penyusunan ulang item seret dan letakkan menggunakan peristiwa seret standar. Posisi tanda penyisipan diperbarui dalam handler untuk peristiwa tersebut Control.DragOver . Dalam handler ini, posisi penunjuk mouse dibandingkan dengan titik tengah item terdekat, dan hasilnya digunakan untuk menentukan apakah tanda penyisipan muncul di sebelah kiri atau kanan item.
Untuk contoh lengkapnya, lihat ListViewInsertionMark topik referensi gambaran umum.
// Moves the insertion mark as the item is dragged.
void myListView_DragOver( Object^ /*sender*/, DragEventArgs^ e )
{
// Retrieve the client coordinates of the mouse pointer.
Point targetPoint = myListView->PointToClient( Point(e->X,e->Y) );
// Retrieve the index of the item closest to the mouse pointer.
int targetIndex = myListView->InsertionMark->NearestIndex( targetPoint );
// Confirm that the mouse pointer is not over the dragged item.
if ( targetIndex > -1 )
{
// Determine whether the mouse pointer is to the left or
// the right of the midpoint of the closest item and set
// the InsertionMark.AppearsAfterItem property accordingly.
Rectangle itemBounds = myListView->GetItemRect( targetIndex );
if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) )
{
myListView->InsertionMark->AppearsAfterItem = true;
}
else
{
myListView->InsertionMark->AppearsAfterItem = false;
}
}
// Set the location of the insertion mark. If the mouse is
// over the dragged item, the targetIndex value is -1 and
// the insertion mark disappears.
myListView->InsertionMark->Index = targetIndex;
}
// Moves the insertion mark as the item is dragged.
private void myListView_DragOver(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the mouse pointer.
Point targetPoint =
myListView.PointToClient(new Point(e.X, e.Y));
// Retrieve the index of the item closest to the mouse pointer.
int targetIndex = myListView.InsertionMark.NearestIndex(targetPoint);
// Confirm that the mouse pointer is not over the dragged item.
if (targetIndex > -1)
{
// Determine whether the mouse pointer is to the left or
// the right of the midpoint of the closest item and set
// the InsertionMark.AppearsAfterItem property accordingly.
Rectangle itemBounds = myListView.GetItemRect(targetIndex);
if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) )
{
myListView.InsertionMark.AppearsAfterItem = true;
}
else
{
myListView.InsertionMark.AppearsAfterItem = false;
}
}
// Set the location of the insertion mark. If the mouse is
// over the dragged item, the targetIndex value is -1 and
// the insertion mark disappears.
myListView.InsertionMark.Index = targetIndex;
}
' Moves the insertion mark as the item is dragged.
Private Sub myListView_DragOver(sender As Object, e As DragEventArgs)
' Retrieve the client coordinates of the mouse pointer.
Dim targetPoint As Point = myListView.PointToClient(New Point(e.X, e.Y))
' Retrieve the index of the item closest to the mouse pointer.
Dim targetIndex As Integer = _
myListView.InsertionMark.NearestIndex(targetPoint)
' Confirm that the mouse pointer is not over the dragged item.
If targetIndex > -1 Then
' Determine whether the mouse pointer is to the left or
' the right of the midpoint of the closest item and set
' the InsertionMark.AppearsAfterItem property accordingly.
Dim itemBounds As Rectangle = myListView.GetItemRect(targetIndex)
If targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) Then
myListView.InsertionMark.AppearsAfterItem = True
Else
myListView.InsertionMark.AppearsAfterItem = False
End If
End If
' Set the location of the insertion mark. If the mouse is
' over the dragged item, the targetIndex value is -1 and
' the insertion mark disappears.
myListView.InsertionMark.Index = targetIndex
End Sub
Keterangan
Metode ini memungkinkan Anda menemukan item yang paling dekat dengan penunjuk mouse saat melakukan operasi seret dan letakkan. Gunakan nilai indeks yang dikembalikan untuk mengatur Index properti . Ketika item yang paling dekat dengan penunjuk mouse adalah item yang diseret, nilai pengembalian metode ini adalah -1. Dalam hal ini, mengatur Index properti ke nilai ini menyembunyikan tanda penyisipan.
Metode ini menemukan item terdekat terlepas dari di mana penunjuk mouse berada, sementara ListView.GetItemAt metode mengembalikan item hanya di lokasi yang ditentukan, atau null
jika tidak ada item di lokasi tersebut. Metode ListView.GetItemAt mengembalikan null
, misalnya, ketika penunjuk mouse terletak di antara dua item. Untuk alasan ini, Anda harus selalu menggunakan NearestIndex metode saat menggunakan operasi seret dan letakkan untuk memposisikan item.
Berlaku untuk
Lihat juga
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk