ListViewInsertionMark.NearestIndex(Point) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取最接近指定點的項目索引。
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
參數
傳回
最接近指定點的項目索引,如果最接近的項目是目前正在拖曳的項目,則為 -1。
範例
下列程式碼範例示範如何使用 ListView 插入標記功能,並使用標準拖曳事件來實作拖放專案重新排序。 插入標記的位置會在事件的處理常式 Control.DragOver 中更新。 在此處理程式中,滑鼠指標的位置會與最接近專案的中間點進行比較,而結果會用來判斷插入標記是否出現在專案的左邊或右邊。
如需完整的範例,請參閱 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
備註
這個方法可讓您在執行拖放作業時,找出最接近滑鼠指標的專案。 使用傳回的索引值來設定 Index 屬性。 當最接近滑鼠指標的專案是要拖曳的專案時,這個方法的傳回值為 -1。 在此情況下,將 屬性設定 Index 為這個值會隱藏插入標記。
這個方法會尋找最接近的專案,不論滑鼠指標位於何處,而 ListView.GetItemAt 方法只會傳回位於指定位置的專案,或 null
如果該位置沒有專案。 方法 ListView.GetItemAt 會傳 null
回 ,例如,當滑鼠指標位於兩個專案之間時。 基於這個理由,使用拖放作業來放置專案時,您應該一律使用 NearestIndex 方法。