ListViewInsertionMark.AppearsAfterItem プロパティ

定義

Index プロパティによって指定されたインデックスを持つ項目の右側に挿入マークを表示するかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the insertion mark appears to the right of the item with the index specified by the Index property.

public:
 property bool AppearsAfterItem { bool get(); void set(bool value); };
public bool AppearsAfterItem { get; set; }
member this.AppearsAfterItem : bool with get, set
Public Property AppearsAfterItem As Boolean

プロパティ値

Index プロパティによって指定されたインデックスを持つ項目の右側に挿入マークを表示する場合は true。それ以外の場合は falsetrue if the insertion mark appears to the right of the item with the index specified by the Index property; otherwise, false. 既定値は、false です。The default is false.

次のコード例は、 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

注釈

NearestIndexメソッドを使用すると、マウスポインターに最も近い項目を見つけることができますが、この項目の前後に挿入マークが表示されるかどうかを判断するには、独自の計算を行う必要があります。The NearestIndex method lets you find the item closest to the mouse pointer, but you must do your own calculations to determine whether the insertion mark should appear before or after this item.

AppearsAfterItemプロパティに必要な値を計算するには、次の手順を実行します。To calculate the value needed for the AppearsAfterItem property, follow these steps:

  1. NearestIndexメソッドを使用して、マウスポインターに最も近い項目のインデックスを取得します。Use the NearestIndex method to retrieve the index of the item closest to the mouse pointer.

  2. 項目の外接する四角形をListView.GetItemRect取得するために、インデックス値をメソッドに渡します。Pass the index value to the ListView.GetItemRect method to retrieve the bounding rectangle of the item.

  3. マウスポインターが外接する四角形の中間点の左側にある場合は、 AppearsAfterItemプロパティをにfalse設定します。それ以外の場合trueはに設定します。If the mouse pointer is located to the left of the midpoint of the bounding rectangle, set the AppearsAfterItem property to false; otherwise, set it to true.

詳細については、 ListViewInsertionMark 「概要リファレンス」トピックを参照してください。For more information, see the ListViewInsertionMark overview reference topic.

適用対象

こちらもご覧ください