Share via


ListViewInsertionMark.NearestIndex(Point) Metodo

Definizione

Recupera l'indice dell'elemento più vicino al punto specificato.

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

Parametri

pt
Point

Oggetto Point che rappresenta la posizione a partire dalla quale individuare l'elemento più vicino.

Restituisce

L'indice dell'elemento più vicino al punto specificato o -1 se l'elemento più vicino è quello attualmente trascinato.

Esempio

Nell'esempio di codice seguente viene illustrato come usare la ListView funzionalità di contrassegno di inserimento e viene implementato il riordinamento degli elementi di trascinamento tramite gli eventi di trascinamento standard. La posizione del segno di inserimento viene aggiornata in un gestore per l'evento Control.DragOver . In questo gestore, la posizione del puntatore del mouse viene confrontata con il punto intermedio dell'elemento più vicino e il risultato viene utilizzato per determinare se il segno di inserimento viene visualizzato a sinistra o a destra dell'elemento.

Per l'esempio completo, vedere l'argomento di ListViewInsertionMark riferimento di panoramica.

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

Commenti

Questo metodo consente di individuare l'elemento più vicino al puntatore del mouse quando si esegue un'operazione di trascinamento della selezione. Utilizzare il valore di indice restituito per impostare la Index proprietà . Quando l'elemento più vicino al puntatore del mouse è l'elemento trascinato, il valore restituito di questo metodo è -1. In questo caso, l'impostazione della Index proprietà su questo valore nasconde il segno di inserimento.

Questo metodo trova l'elemento più vicino indipendentemente dalla posizione in cui si trova il puntatore del mouse, mentre il ListView.GetItemAt metodo restituisce l'elemento solo nella posizione specificata o null se non è presente alcun elemento in tale posizione. Il ListView.GetItemAt metodo restituisce null, ad esempio, quando il puntatore del mouse si trova tra due elementi. Per questo motivo, è consigliabile usare sempre il NearestIndex metodo quando si usa un'operazione di trascinamento della selezione per posizionare gli elementi.

Si applica a

Vedi anche