ListViewInsertionMark.AppearsAfterItem 属性

定义

获取或设置一个值,该值指示是否在项的右侧显示插入标记,该项带有 Index 属性指定的索引。

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;否则为 false。 默认值为 false

示例

下面的代码示例演示如何使用 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

注解

方法 NearestIndex 允许您查找最靠近鼠标指针的项,但您必须执行自己的计算,以确定插入标记应出现在此项之前还是之后。

若要计算 属性所需的 AppearsAfterItem 值,请执行以下步骤:

  1. NearestIndex使用 方法检索最靠近鼠标指针的项的索引。

  2. 将索引值传递给 方法以 ListView.GetItemRect 检索项的边框。

  3. 如果鼠标指针位于边框的中点左侧,请将 属性设置为 AppearsAfterItemfalse;否则,将其设置为 true

有关详细信息,请参阅 ListViewInsertionMark 概述参考主题。

适用于

另请参阅