ListViewInsertionMark.NearestIndex(Point) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Haalt de index op van het item dat het dichtst bij het opgegeven punt ligt.
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
Parameters
- pt
- Point
Een Point weergave van de locatie van waaruit het dichtstbijzijnde item moet worden gevonden.
Retouren
De index van het item dat het dichtst bij het opgegeven punt ligt of -1 als het dichtstbijzijnde item het item is dat momenteel wordt gesleept.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de ListView functie voor het invoegen van markeringen gebruikt en het opnieuw ordenen van items met slepen en neerzetten implementeert met behulp van de standaardsleepgebeurtenissen. De positie van de invoegmarkering wordt bijgewerkt in een handler voor de Control.DragOver gebeurtenis. In deze handler wordt de positie van de muisaanwijzer vergeleken met het middelpunt van het dichtstbijzijnde item en wordt het resultaat gebruikt om te bepalen of de invoegmarkering links of rechts van het item wordt weergegeven.
Zie het overzichtsonderwerp voor het ListViewInsertionMark volledige voorbeeld.
// 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
Opmerkingen
Met deze methode kunt u het item het dichtst bij de muis aanwijzer vinden wanneer u een slepen-en-neerzetten-bewerking uitvoert. Gebruik de indexwaarde die wordt geretourneerd om de Index eigenschap in te stellen. Wanneer het item het dichtst bij de muis aanwijzer ligt, wordt het item dat wordt gesleept, de retourwaarde van deze methode -1. In dit geval verbergt het instellen van de Index eigenschap op deze waarde de invoegmarkering.
Met deze methode wordt het dichtstbijzijnde item gevonden, ongeacht waar de muiswijzer zich bevindt, terwijl de ListView.GetItemAt methode het item alleen op de opgegeven locatie retourneert of null als er geen item op die locatie is. De ListView.GetItemAt methode retourneert nullbijvoorbeeld wanneer de muiswijzer zich tussen twee items bevindt. Daarom moet u altijd de NearestIndex methode gebruiken wanneer u een slepen-en-neerzetten-bewerking gebruikt om items te positioneren.