ListViewInsertionMark.NearestIndex(Point) メソッド

定義

指定した位置に最も近い項目のインデックスを取得します。Retrieves the index of the item closest to the specified point.

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

パラメーター

pt
Point

最も近い項目を探すときの起点を表す PointA Point representing the location from which to find the nearest item.

戻り値

指定された位置に最も近い項目のインデックス。最も近い項目が、現在ドラッグしている項目である場合は、-1 が返されます。The index of the item closest to the specified point or -1 if the closest item is the item currently being dragged.

次のコード例は、 ListView挿入マーク機能を使用し、標準のドラッグイベントを使用してドラッグアンドドロップ項目の並べ替えを実装する方法を示しています。The following code example demonstrates how to use the ListView insertion mark feature and implements drag-and-drop item reordering using the standard drag events. 挿入マークの位置は、 Control.DragOverイベントのハンドラーで更新されます。The position of the insertion mark is updated in a handler for the Control.DragOver event. このハンドラーでは、マウスポインターの位置は最も近い項目の中間点と比較され、結果は、挿入マークが項目の左側または右側に表示されるかどうかを判断するために使用されます。In this handler, the position of the mouse pointer is compared to the midpoint of the nearest item, and the result is used to determine whether the insertion mark appears to the left or the right of the item.

完全な例についてはListViewInsertionMark 、「概要リファレンス」トピックを参照してください。For the complete example, see the ListViewInsertionMark overview reference topic.

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

注釈

このメソッドを使用すると、ドラッグアンドドロップ操作の実行時にマウスポインターに最も近い項目を見つけることができます。This method lets you locate the item closest to the mouse pointer when doing a drag-and-drop operation. 返されたインデックス値を使用しIndexて、プロパティを設定します。Use the index value returned to set the Index property. マウスポインターに最も近い項目がドラッグされている場合、このメソッドの戻り値は-1 になります。When the item closest to the mouse pointer is the item being dragged, the return value of this method is -1. この場合、 Indexプロパティをこの値に設定すると、挿入マークが非表示になります。In this case, setting the Index property to this value hides the insertion mark.

このメソッドはマウス ポインターがある場所に関係なく、最も近いアイテムを検出しますが、ListView.GetItemAt メソッドは、指定された場所のアイテムのみを返し、その場所にアイテムがない場合はnullを返します。This method finds the closest item regardless of where the mouse pointer is located, while the ListView.GetItemAt method returns the item at the specified location only, or null if there is no item at that location. ListView.GetItemAt メソッドは、たとえばマウス ポインターが 2 つのアイテムの間にある場合にはnullを返します。The ListView.GetItemAt method returns null, for example, when the mouse pointer is located between two items. この理由から、ドラッグ アンド ドロップ操作を使用してアイテムを配置する場合には、必ず NearestIndex メソッドを使用してください。For this reason, you should always use the NearestIndex method when using a drag-and-drop operation to position items.

適用対象

こちらもご覧ください