ListViewInsertionMark.NearestIndex(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

最も近い項目を探すときの起点を表す Point

戻り値

指定された位置に最も近い項目のインデックス。最も近い項目が、現在ドラッグしている項目である場合は、-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 メソッドは、たとえばマウス ポインターが 2 つのアイテムの間にある場合にはnullを返します。 この理由から、ドラッグ アンド ドロップ操作を使用してアイテムを配置する場合には、必ず NearestIndex メソッドを使用してください。

適用対象

こちらもご覧ください