英語で読む

次の方法で共有


ListView.InsertionMark プロパティ

定義

ListView コントロール内で項目がドラッグされる場合に目的のドロップ位置を示すために使用されるオブジェクトを取得します。

[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewInsertionMark InsertionMark { get; }

プロパティ値

挿入マークを表す ListViewInsertionMark オブジェクト。

属性

次のコード例は、挿入マーク機能の使用方法を ListView 示しています。 この例では、標準のドラッグ イベントを使用して、項目の並べ替えのドラッグ アンド ドロップを実装します。 イベントのハンドラーで挿入マークの位置が更新されます Control.DragOver 。 このハンドラーでは、マウス ポインターの位置が最も近い項目の中間点と比較され、結果を使用して、挿入マークが項目の左または右に表示されるかどうかを判断します。

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

注釈

ListView挿入マーク機能を使用すると、項目が新しい位置にドラッグされたときに、ドラッグ アンド ドロップ操作で予想されるドロップ位置を視覚的に示すことができます。 この機能は、 プロパティが AutoArrangetrue 設定されている場合と、コントロールが項目を ListView 自動的に並べ替えない場合にのみ機能します。 自動並べ替えを防ぐには、 プロパティを SortingSortOrder.None設定し、 プロパティを View 、、View.SmallIconまたは View.Tileに設定するView.LargeIcon必要があります。 さらに、グループ化機能はグループ メンバーシップによって項目を ListView 並べ替えるため、挿入マーク機能がグループ化機能と共に表示されない場合があります。

クラスはListViewInsertionMark通常、 イベントまたは Control.MouseMove イベントのハンドラーControl.DragOverで使用され、項目がドラッグされるときに挿入マークの位置を更新します。 また、 イベントまたは Control.MouseUp イベントのハンドラーControl.DragDropでも使用され、ドラッグされた項目を正しい場所に挿入します。 詳細については、「方法: Windows フォーム ListView コントロールに挿入マークを表示する」を参照してくださいListViewInsertionMark

注意

挿入マーク機能は、アプリケーションが メソッドを呼び出 Application.EnableVisualStyles すときに、Windows XP および Windows Server 2003 でのみ使用できます。 以前のオペレーティング システムでは、挿入マークに関連するすべてのコードは効果がなく、挿入マークは表示されません。 その結果、挿入マーク機能に依存するコードが正しく機能しない可能性があります。 この機能を使用できるかどうかを決定するコードを含め、使用できない場合は代替機能を提供する必要がある場合があります。 たとえば、挿入マークをサポートしていないオペレーティング システムで実行するときに、ドラッグ アンド ドロップ項目の再配置を実装するすべてのコードをバイパスできます。

挿入マーク機能は、オペレーティング システムのテーマ機能を提供するのと同じライブラリによって提供されます。 このライブラリの可用性をチェックするには、 メソッド オーバーロードをFeatureSupport.IsPresent(Object)呼び出して 値をOSFeature.Themes渡します。

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください