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 статье.

Применяется к

См. также раздел