Nyelv

ListViewInsertionMark.NearestIndex(Point) Metódus

Definíció

A megadott ponthoz legközelebb eső elem indexét kéri le.

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

Paraméterek

pt
Point

Azt Point a helyet jelöli, ahonnan a legközelebbi elemet meg szeretné keresni.

Válaszok

A megadott ponthoz legközelebb eső elem indexe, vagy -1, ha a legközelebbi elem az éppen húzott elem.

Példák

Az alábbi példakód bemutatja, hogyan használható a ListView beszúrási jel funkció, és hogyan valósítható meg a húzási elemek átrendezése a szokásos húzási események használatával. A beszúrási jel pozíciója frissül az Control.DragOver esemény kezelőjében. Ebben a kezelőben az egérmutató pozíciója a legközelebbi elem középpontjának összehasonlítása, és az eredmény annak meghatározására szolgál, hogy a beszúrási jel az elem bal vagy jobb oldalán jelenik-e meg.

A teljes példáért tekintse meg az ListViewInsertionMark áttekintési referenciatémakört.

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

Megjegyzések

Ez a módszer lehetővé teszi az egérmutatóhoz legközelebb eső elem megkeresését húzási művelet közben. A tulajdonság beállításához használja a visszaadott indexértéket Index . Ha az egérmutatóhoz legközelebb eső elem az éppen húzott elem, a metódus visszatérési értéke -1. Ebben az esetben a Index tulajdonság ilyen értékre állítása elrejti a beszúrási jelet.

Ez a metódus az egérmutató helyétől függetlenül megkeresi a legközelebbi elemet, míg a ListView.GetItemAt metódus csak a megadott helyen adja vissza az elemet, vagy null ha nincs elem ezen a helyen. A ListView.GetItemAt metódus például akkor adja vissza null, ha az egérmutató két elem között található. Ezért mindig használja a metódust húzással NearestIndex az elemek elhelyezéséhez.

A következőre érvényes:

Lásd még