ListViewInsertionMark 클래스

정의

항목을 ListView 컨트롤 내의 새 위치로 끌어서 놓을 때 예상되는 위치를 나타내는 데 사용됩니다. 이 기능은 Windows XP 이상에서만 사용할 수 있습니다.

public ref class ListViewInsertionMark sealed
public sealed class ListViewInsertionMark
type ListViewInsertionMark = class
Public NotInheritable Class ListViewInsertionMark
상속
ListViewInsertionMark

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 ListView 삽입 표시 기능 및 구현 항목 끌어서 놓기 재정렬 표준을 사용 하 여 끌기 이벤트입니다. 삽입 표시의 위치에 대 한 처리기에서 업데이트 되는 Control.DragOver 이벤트입니다. 이 처리기에서 가장 가까운 항목의 중간점 마우스 포인터의 위치를 비교 하 고 결과 삽입 표시가 항목의 오른쪽 또는 왼쪽에 표시 되는지 여부를 확인 하려면 사용 됩니다.

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class ListViewInsertionMarkExample: public Form
{
private:
   ListView^ myListView;

public:

   ListViewInsertionMarkExample()
   {
      // Initialize myListView.
      myListView = gcnew ListView;
      myListView->Dock = DockStyle::Fill;
      myListView->View = View::LargeIcon;
      myListView->MultiSelect = false;
      myListView->ListViewItemSorter = gcnew ListViewIndexComparer;

      // Initialize the insertion mark.
      myListView->InsertionMark->Color = Color::Green;

      // Add items to myListView.
      myListView->Items->Add( "zero" );
      myListView->Items->Add( "one" );
      myListView->Items->Add( "two" );
      myListView->Items->Add( "three" );
      myListView->Items->Add( "four" );
      myListView->Items->Add( "five" );

      // Initialize the drag-and-drop operation when running
      // under Windows XP or a later operating system.
      if ( System::Environment::OSVersion->Version->Major > 5 || (System::Environment::OSVersion->Version->Major == 5 && System::Environment::OSVersion->Version->Minor >= 1) )
      {
         myListView->AllowDrop = true;
         myListView->ItemDrag += gcnew ItemDragEventHandler( this, &ListViewInsertionMarkExample::myListView_ItemDrag );
         myListView->DragEnter += gcnew DragEventHandler( this, &ListViewInsertionMarkExample::myListView_DragEnter );
         myListView->DragOver += gcnew DragEventHandler( this, &ListViewInsertionMarkExample::myListView_DragOver );
         myListView->DragLeave += gcnew EventHandler( this, &ListViewInsertionMarkExample::myListView_DragLeave );
         myListView->DragDrop += gcnew DragEventHandler( this, &ListViewInsertionMarkExample::myListView_DragDrop );
      }

      // Initialize the form.
      this->Text = "ListView Insertion Mark Example";
      this->Controls->Add( myListView );
   }

private:

   // Starts the drag-and-drop operation when an item is dragged.
   void myListView_ItemDrag( Object^ /*sender*/, ItemDragEventArgs^ e )
   {
      myListView->DoDragDrop( e->Item, DragDropEffects::Move );
   }

   // Sets the target drop effect.
   void myListView_DragEnter( Object^ /*sender*/, DragEventArgs^ e )
   {
      e->Effect = e->AllowedEffect;
   }

   // 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;
   }

   // Removes the insertion mark when the mouse leaves the control.
   void myListView_DragLeave( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myListView->InsertionMark->Index = -1;
   }

   // Moves the item to the location of the insertion mark.
   void myListView_DragDrop( Object^ /*sender*/, DragEventArgs^ e )
   {
      // Retrieve the index of the insertion mark;
      int targetIndex = myListView->InsertionMark->Index;

      // If the insertion mark is not visible, exit the method.
      if ( targetIndex == -1 )
      {
         return;
      }

      // If the insertion mark is to the right of the item with
      // the corresponding index, increment the target index.
      if ( myListView->InsertionMark->AppearsAfterItem )
      {
         targetIndex++;
      }

      // Retrieve the dragged item.
      ListViewItem^ draggedItem = dynamic_cast<ListViewItem^>(e->Data->GetData( ListViewItem::typeid ));

      // Insert a copy of the dragged item at the target index.
      // A copy must be inserted before the original item is removed
      // to preserve item index values.
      myListView->Items->Insert( targetIndex, dynamic_cast<ListViewItem^>(draggedItem->Clone()) );

      // Remove the original copy of the dragged item.
      myListView->Items->Remove( draggedItem );

   }

   // Sorts ListViewItem objects by index.
   ref class ListViewIndexComparer: public System::Collections::IComparer
   {
   public:
      virtual int Compare( Object^ x, Object^ y )
      {
         return (dynamic_cast<ListViewItem^>(x))->Index - (dynamic_cast<ListViewItem^>(y))->Index;
      }
   };
};

[STAThread]
int main()
{
   Application::EnableVisualStyles();
   Application::Run( gcnew ListViewInsertionMarkExample );
}
using System;
using System.Drawing;
using System.Windows.Forms;

public class ListViewInsertionMarkExample : Form
{
    private ListView myListView; 

    public ListViewInsertionMarkExample()
    {
        // Initialize myListView.
        myListView = new ListView();
        myListView.Dock = DockStyle.Fill;
        myListView.View = View.LargeIcon;
        myListView.MultiSelect = false;
        myListView.ListViewItemSorter = new ListViewIndexComparer();

        // Initialize the insertion mark.
        myListView.InsertionMark.Color = Color.Green;

        // Add items to myListView.
        myListView.Items.Add("zero");
        myListView.Items.Add("one");
        myListView.Items.Add("two");
        myListView.Items.Add("three");
        myListView.Items.Add("four");
        myListView.Items.Add("five");
        
        // Initialize the drag-and-drop operation when running
        // under Windows XP or a later operating system.
        if (OSFeature.Feature.IsPresent(OSFeature.Themes))
        {
            myListView.AllowDrop = true;
            myListView.ItemDrag += new ItemDragEventHandler(myListView_ItemDrag);
            myListView.DragEnter += new DragEventHandler(myListView_DragEnter);
            myListView.DragOver += new DragEventHandler(myListView_DragOver);
            myListView.DragLeave += new EventHandler(myListView_DragLeave);
            myListView.DragDrop += new DragEventHandler(myListView_DragDrop);
        }

        // Initialize the form.
        this.Text = "ListView Insertion Mark Example";
        this.Controls.Add(myListView);
    }

    [STAThread]
    static void Main() 
    {
        Application.EnableVisualStyles();
        Application.Run(new ListViewInsertionMarkExample());
    }

    // Starts the drag-and-drop operation when an item is dragged.
    private void myListView_ItemDrag(object sender, ItemDragEventArgs e)
    {
        myListView.DoDragDrop(e.Item, DragDropEffects.Move);
    }

    // Sets the target drop effect.
    private void myListView_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = e.AllowedEffect;
    }

    // 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;
    }

    // Removes the insertion mark when the mouse leaves the control.
    private void myListView_DragLeave(object sender, EventArgs e)
    {
        myListView.InsertionMark.Index = -1;
    }

    // Moves the item to the location of the insertion mark.
    private void myListView_DragDrop(object sender, DragEventArgs e)
    {
        // Retrieve the index of the insertion mark;
        int targetIndex = myListView.InsertionMark.Index;

        // If the insertion mark is not visible, exit the method.
        if (targetIndex == -1) 
        {
            return;
        }

        // If the insertion mark is to the right of the item with
        // the corresponding index, increment the target index.
        if (myListView.InsertionMark.AppearsAfterItem) 
        {
            targetIndex++;
        }

        // Retrieve the dragged item.
        ListViewItem draggedItem = 
            (ListViewItem)e.Data.GetData(typeof(ListViewItem));

        // Insert a copy of the dragged item at the target index.
        // A copy must be inserted before the original item is removed
        // to preserve item index values. 
        myListView.Items.Insert(
            targetIndex, (ListViewItem)draggedItem.Clone());

        // Remove the original copy of the dragged item.
        myListView.Items.Remove(draggedItem);
    }

    // Sorts ListViewItem objects by index.
    private class ListViewIndexComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
        }
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public Class ListViewInsertionMarkExample
    Inherits Form

    Private myListView As ListView
    
    Public Sub New()
        ' Initialize myListView.
        myListView = New ListView()
        myListView.Dock = DockStyle.Fill
        myListView.View = View.LargeIcon
        myListView.MultiSelect = False
        myListView.ListViewItemSorter = New ListViewIndexComparer()
        
        ' Initialize the insertion mark.
        myListView.InsertionMark.Color = Color.Green
        
        ' Add items to myListView.
        myListView.Items.Add("zero")
        myListView.Items.Add("one")
        myListView.Items.Add("two")
        myListView.Items.Add("three")
        myListView.Items.Add("four")
        myListView.Items.Add("five")
        
        ' Initialize the drag-and-drop operation when running
        ' under Windows XP or a later operating system.
        If OSFeature.Feature.IsPresent(OSFeature.Themes)
            myListView.AllowDrop = True
            AddHandler myListView.ItemDrag, AddressOf myListView_ItemDrag
            AddHandler myListView.DragEnter, AddressOf myListView_DragEnter
            AddHandler myListView.DragOver, AddressOf myListView_DragOver
            AddHandler myListView.DragLeave, AddressOf myListView_DragLeave
            AddHandler myListView.DragDrop, AddressOf myListView_DragDrop
        End If 

        ' Initialize the form.
        Me.Text = "ListView Insertion Mark Example"
        Me.Controls.Add(myListView)
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New ListViewInsertionMarkExample())
    End Sub
    
    ' Starts the drag-and-drop operation when an item is dragged.
    Private Sub myListView_ItemDrag(sender As Object, e As ItemDragEventArgs)
        myListView.DoDragDrop(e.Item, DragDropEffects.Move)
    End Sub
    
    ' Sets the target drop effect.
    Private Sub myListView_DragEnter(sender As Object, e As DragEventArgs)
        e.Effect = e.AllowedEffect
    End Sub
    
    ' 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

    ' Removes the insertion mark when the mouse leaves the control.
    Private Sub myListView_DragLeave(sender As Object, e As EventArgs)
        myListView.InsertionMark.Index = -1
    End Sub
    
    ' Moves the item to the location of the insertion mark.
    Private Sub myListView_DragDrop(sender As Object, e As DragEventArgs)
        ' Retrieve the index of the insertion mark;
        Dim targetIndex As Integer = myListView.InsertionMark.Index
        
        ' If the insertion mark is not visible, exit the method.
        If targetIndex = -1 Then
            Return
        End If 

        ' If the insertion mark is to the right of the item with
        ' the corresponding index, increment the target index.
        If myListView.InsertionMark.AppearsAfterItem Then
            targetIndex += 1
        End If 

        ' Retrieve the dragged item.
        Dim draggedItem As ListViewItem = _
            CType(e.Data.GetData(GetType(ListViewItem)), ListViewItem)
        
        ' Insert a copy of the dragged item at the target index.
        ' A copy must be inserted before the original item is removed
        ' to preserve item index values.
        myListView.Items.Insert(targetIndex, _
            CType(draggedItem.Clone(), ListViewItem))
        
        ' Remove the original copy of the dragged item.
        myListView.Items.Remove(draggedItem)

    End Sub
    
    ' Sorts ListViewItem objects by index.    
    Private Class ListViewIndexComparer
        Implements System.Collections.IComparer
        
        Public Function Compare(x As Object, y As Object) As Integer _
            Implements System.Collections.IComparer.Compare
            Return CType(x, ListViewItem).Index - CType(y, ListViewItem).Index
        End Function 'Compare

    End Class

End Class

설명

검색할 수 있습니다는 ListViewInsertionMark 에서 InsertionMark 의 속성을 ListView 제어 하 고 항목을 새 위치로 끌 때 끌어서 놓기 작업에서 예상 되는 놓을 위치를 시각적으로 나타내는 데 사용 합니다.

이 기능은 경우에만 작동 합니다 ListView.AutoArrange 속성이로 설정 되어 true 및 시기를 ListView 컨트롤 항목을 자동으로 정렬 되지 않습니다. 자동 정렬 방지 하기 위해 합니다 ListView.Sorting 속성으로 설정 되어 있어야 SortOrder.NoneListView.View 속성 설정 해야 합니다 View.LargeIcon, View.SmallIcon, 또는 View.Tile합니다. 삽입 표시 기능은 함께 사용할 수 없습니다 또한는 ListView 그룹화 기능은 그룹 멤버 자격을 기준으로 항목을 정렬 하기 때문에 기능을 그룹화 합니다.

ListViewInsertionMark 클래스에 대 한 처리기에서 일반적으로 사용 됩니다 합니다 Control.DragOver 또는 Control.MouseMove 이벤트 항목을 끄는 대로 삽입 표시의 위치를 업데이트 합니다. 에 대 한 처리기에서 사용 되는 합니다 Control.DragDrop 또는 Control.MouseUp 끌어 온된 항목을 올바른 위치에 삽입 하는 이벤트입니다.

삽입 표시의 위치를 업데이트 하려면 다음이 단계를 수행 합니다.

  1. 대 한 처리기를 Control.DragOver 또는 Control.MouseMove 이벤트를 사용 하 여는 ListView.InsertionMark 속성에 액세스를 ListViewInsertionMark 연관 된 개체는 ListView 컨트롤.

  2. 사용 된 NearestIndex 마우스 포인터에 대 한 가장 가까운 항목의 인덱스를 검색 하는 방법입니다.

  3. 인덱스 값을 전달 합니다 ListView.GetItemRect 항목의 경계 사각형을 검색 하는 방법입니다.

  4. 마우스 포인터의 경계 사각형의 중간점 왼쪽에 있는 경우, 설정 된 AppearsAfterItem 속성을 false이 고, 그렇지 않으면로 설정 true합니다.

  5. 설정 된 Index 속성에서 검색 된 인덱스 값을는 NearestIndex 메서드. 삽입 표시가 나타나지 왼쪽 또는 오른쪽에 따라 지정된 된 인덱스를 사용 하 여 항목 옆에 AppearsAfterItem 속성 값입니다. 자체를 통해 항목을 끌어 인덱스가-1 이며 삽입 표시가 나타나지 않습니다.

끌어 온된 항목에 올바른 위치에 삽입 하려면 다음이 단계를 수행 합니다.

  1. 에 대 한 처리기에는 Control.DragDrop 또는 Control.MouseUp 이벤트를 사용 하 여는 Index 삽입 표시의 현재 위치를 결정 하는 속성입니다. 삽입 인덱스와 나중에 사용 하도록이 값을 저장 합니다.

  2. 경우는 AppearsAfterItem 속성이 true, 저장된 삽입 인덱스 값이 증가 합니다.

  3. 사용 하 여는 ListView.ListViewItemCollection.Insert 으로 끌어 온된 항목의 복제본을 삽입 하는 방법은 ListView.Items 저장된 삽입 인덱스의 컬렉션입니다.

  4. 사용 된 ListView.ListViewItemCollection.Remove 끌어 온된 항목의 원래 복사본을 제거 하는 방법입니다.

원래 복사본에는 인덱스 값 하므로 제거 되기 전까지 끌어 온된 항목의 복제본을 삽입 해야 합니다는 ListView.Items 삽입 하기 전에 컬렉션은 변경 되지 않습니다.

설정 항목을 인덱스 값과 동일한 순서로 표시 되는지 확인을 해야 합니다는 ListView.ListViewItemSorter 속성의 구현에는 IComparer 항목을 인덱스 값으로 정렬 하는 인터페이스입니다. 자세한 내용은 예제 섹션을 참조 하세요.

사용 하 여 삽입 표시의 색을 수정할 수는 Color 속성입니다. 크기 또는 삽입 표시의 위치를 해야 하는 경우 해당 경계 사각형을 통해 가져올 수 있습니다는 Bounds 속성입니다.

참고

삽입 표시 기능은 Windows XP 및 Windows Server 2003 제품군 에서만 사용할 수 있는 애플리케이션 호출을 Application.EnableVisualStyles 메서드. 이전 운영 체제에서는 삽입 표시와 관련 된 모든 코드가 무시 되 고 삽입 표시가 나타나지 않습니다. 결과적으로, 삽입 표시 기능에 종속 된 모든 코드가 제대로 작동 하지 않을 수 있습니다. 삽입 표시 기능을 사용할 수 있는지 여부를 확인 하는 테스트를 포함 하 고 사용할 수 없을 때 대체 기능을 제공할 수 있습니다. 예를 들어 다음 삽입 표시를 지원 하지 않는 운영 체제에서 실행 하는 경우 위치를 변경 하는 끌어서 놓기 항목을 구현 하는 모든 코드를 무시 하는 것이 좋습니다.

삽입 표시 기능은 운영 체제 테마 기능을 제공 하는 라이브러리에서 제공 됩니다. 이 라이브러리의 가용성을 확인 하려면 호출을 FeatureSupport.IsPresent(Object) 메서드 오버 로드 하 고 전달 합니다 OSFeature.Themes 값입니다.

속성

AppearsAfterItem

삽입 표시가 Index 속성으로 지정된 인덱스에 있는 항목의 오른쪽에 나타나는지 여부를 나타내는 값을 가져오거나 설정합니다.

Bounds

삽입 표시의 경계 사각형을 가져옵니다.

Color

삽입 표시의 색을 가져오거나 설정합니다.

Index

삽입 표시 옆에 나타나는 항목의 인덱스를 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
NearestIndex(Point)

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

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보