ListViewInsertionMark.NearestIndex(Point) Metoda

Definicja

Pobiera indeks elementu znajdującego się najbliżej określonego punktu.

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

pt
Point

Reprezentująca Point lokalizację, z której można znaleźć najbliższy element.

Zwraca

Indeks elementu znajdującego się najbliżej określonego punktu lub -1, jeśli najbliższy element jest aktualnie przeciągnięty.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać ListView funkcji znacznika wstawiania i implementuje zmiany kolejności elementów przeciągania i upuszczania przy użyciu standardowych zdarzeń przeciągania. Pozycja znacznika wstawiania jest aktualizowana w procedurze obsługi zdarzenia Control.DragOver . W tej procedurze obsługi położenie wskaźnika myszy jest porównywane z punktem środkowym najbliższego elementu, a wynik służy do określenia, czy znacznik wstawiania pojawia się po lewej stronie, czy po prawej stronie elementu.

Aby zapoznać się z kompletnym przykładem, zobacz ListViewInsertionMark temat przeglądu.

// 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

Uwagi

Ta metoda umożliwia zlokalizowanie elementu znajdującego się najbliżej wskaźnika myszy podczas wykonywania operacji przeciągania i upuszczania. Użyj wartości indeksu zwróconej, aby ustawić Index właściwość. Gdy element najbliżej wskaźnika myszy jest elementem przeciąganym, zwracana wartość tej metody to -1. W takim przypadku ustawienie Index właściwości na tę wartość powoduje ukrycie znacznika wstawiania.

Ta metoda znajduje najbliższy element niezależnie od tego, gdzie znajduje się wskaźnik myszy, podczas gdy ListView.GetItemAt metoda zwraca element tylko w określonej lokalizacji lub null jeśli w tej lokalizacji nie ma żadnego elementu. Metoda ListView.GetItemAt zwraca nullwartość , na przykład gdy wskaźnik myszy znajduje się między dwoma elementami. Z tego powodu należy zawsze używać NearestIndex metody podczas używania operacji przeciągania i upuszczania, aby umieścić elementy.

Dotyczy

Zobacz też