通过


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

属性值

true 如果插入标记显示在具有属性指定的 Index 索引的项右侧,则为 ;否则为 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. 如果鼠标指针位于边界矩形的中点左侧,请将 AppearsAfterItem 属性设置为 false;否则,将其设置为 true

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

适用于

另请参阅