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 概觀參考主題。

適用於

另請參閱