ListViewInsertionMark.NearestIndex(Point) 메서드

정의

지정된 위치에 가장 가까운 항목의 인덱스를 검색합니다.

public:
 int NearestIndex(System::Drawing::Point pt);
public int NearestIndex (System.Drawing.Point pt);
member this.NearestIndex : System.Drawing.Point -> int
Public Function NearestIndex (pt As Point) As Integer

매개 변수

pt
Point

가장 가까운 항목을 찾을 위치를 나타내는 Point입니다.

반환

지정된 위치에 가장 가까운 항목의 인덱스이거나 현재 끌고 있는 항목이 가장 가까운 항목인 경우 -1입니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 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

설명

이 메서드를 사용 하면 끌어서 놓기 작업을 수행 하는 경우 마우스 포인터에 대 한 가장 가까운 항목을 찾을 수 있습니다. 설정할 인덱스 값을 반환 하는 사용 된 Index 속성입니다. 마우스 포인터에 대 한 가장 가까운 항목 끌고 있는 항목이 되 면이 메서드의 반환 값은-1입니다. 이 경우에 설정 된 Index 속성이 값이 삽입 표시를 숨깁니다.

이 메서드는 마우스 포인터가 위치한 하는 동안에 관계 없이 가장 가까운 항목을 찾습니다 합니다 ListView.GetItemAt 지정된 된 위치에만 있는 항목을 반환 하는 메서드 또는 null 해당 위치에 항목이 없는 경우. 합니다 ListView.GetItemAt 메서드가 반환 되는 null, 마우스 포인터 두 항목 사이 있는 경우 예를 들어, 합니다. 이러한 이유로 항상 사용 해야 합니다 NearestIndex 메서드 항목 위치로 끌어서 놓기 작업을 사용 하는 경우.

적용 대상

추가 정보