ListViewInsertionMark.NearestIndex(Point) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Načte index položky nejblíže zadanému bodu.
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
Parametry
Návraty
Index položky nejblíže zadanému bodu nebo -1, pokud je položka aktuálně přetahovaná.
Příklady
Následující příklad kódu ukazuje, jak použít ListView funkci značky vložení a implementovat přetahování položek přeuspořádání pomocí standardních událostí přetažení. Pozice značky vložení se aktualizuje v obslužné rutině Control.DragOver události. V této obslužné rutině se pozice ukazatele myši porovná s prostředním bodem nejbližší položky a výsledek slouží k určení, zda se značka vložení zobrazuje vlevo nebo vpravo od položky.
Úplný příklad najdete v referenčním tématu přehledu ListViewInsertionMark .
// 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
Poznámky
Tato metoda umožňuje najít položku nejblíže ukazateli myši při provádění operace přetažení. K nastavení vlastnosti použijte vrácenou hodnotu indexu Index . Pokud je položka, která je nejblíže ukazateli myši, přetahování položky, návratová hodnota této metody je -1. V tomto případě nastavení Index vlastnosti na tuto hodnotu skryje značku vložení.
Tato metoda najde nejbližší položku bez ohledu na umístění ukazatele myši, zatímco ListView.GetItemAt metoda vrátí položku pouze v zadaném umístění nebo null pokud v tomto umístění není žádná položka. Metoda ListView.GetItemAt vrátí nullnapříklad, když je ukazatel myši umístěn mezi dvěma položkami. Z tohoto důvodu byste vždy měli použít metodu NearestIndex při použití operace přetažení k umístění položek.