ListViewInsertionMark Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Arra szolgál, hogy jelezze a várt esési helyet, amikor egy elemet egy vezérlő új helyére ListView húz. Ez a funkció csak Windows XP és újabb verziókban érhető el.
public ref class ListViewInsertionMark sealed
public sealed class ListViewInsertionMark
type ListViewInsertionMark = class
Public NotInheritable Class ListViewInsertionMark
- Öröklődés
-
ListViewInsertionMark
Példák
Az alábbi példakód bemutatja, hogyan használható a ListView beszúrási jel funkció, és hogyan valósítható meg a húzási elemek átrendezése a szokásos húzási események használatával. A beszúrási jel pozíciója frissül az Control.DragOver esemény kezelőjében. Ebben a kezelőben az egérmutató pozíciója a legközelebbi elem középpontjának összehasonlítása, és az eredmény annak meghatározására szolgál, hogy a beszúrási jel az elem bal vagy jobb oldalán jelenik-e meg.
#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
Megjegyzések
Lekérhet egy vezérlőelem tulajdonságából ListViewInsertionMarkInsertionMark egy ListView vezérlőt, és vizuálisan jelezheti az húzásos műveletet, amikor egy elemet új pozícióba húz.
Ez a funkció csak akkor működik, ha a ListView.AutoArrange tulajdonság be van állítva, true és ha a ListView vezérlőelem nem rendezi automatikusan az elemeket. Az automatikus rendezés megakadályozása érdekében a ListView.Sorting tulajdonságot SortOrder.None be kell állítani, a ListView.View tulajdonságot pedig a következőre kell állítani View.LargeIcon: , View.SmallIconvagy View.Tile. Emellett a beszúrási jel funkció nem használható a ListView csoportosítási funkcióval, mert a csoportosítási funkció csoporttagság alapján rendeli meg az elemeket.
Az ListViewInsertionMark osztályt általában az adott vagy Control.DragOver esemény kezelőjében Control.MouseMove használják a beszúrási jel elem húzással való pozíciójának frissítéséhez. Egy kezelőben is használható arra, hogy az vagy Control.DragDrop az Control.MouseUp esemény a megfelelő helyre szúrjon be egy húzott elemet.
A beszúrási jel pozíciójának frissítéséhez kövesse az alábbi lépéseket:
Control.DragOver Az adott vagy Control.MouseMove esemény kezelőjében használja a ListView.InsertionMark tulajdonságot a ListViewInsertionMark vezérlőelemhez ListView társított objektum eléréséhez.
A metódussal NearestIndex lekérheti az egérmutatóhoz legközelebbi elem indexét.
Adja át az indexértéket a metódusnak az ListView.GetItemRect elem határolókeretének lekéréséhez.
Ha az egérmutató a határoló téglalap középpontjától balra található, állítsa a AppearsAfterItem tulajdonságot
false; ellenkező esetben állítsa be a következőretrue: .Állítsa a tulajdonságot Index a metódusból lekért indexértékre NearestIndex . A beszúrási jel a megadott indexet tartalmazó elem mellett jelenik meg a tulajdonság értékétől függően AppearsAfterItem balra vagy jobbra. Ha egy elemet maga húz át, az index -1, és a beszúrási jel el van rejtve.
Ha a megfelelő helyre szeretné beszúrni a húzott elemet, kövesse az alábbi lépéseket:
Az esemény vagy Control.DragDrop esemény Control.MouseUp kezelőjében a Index tulajdonság használatával állapítsa meg a beszúrási jel aktuális helyét. Tárolja ezt az értéket későbbi beszúrási indexként.
Ha a AppearsAfterItem tulajdonság értéke a következő,
truenövelje a tárolt beszúrási index értékét.Ezzel a ListView.ListViewItemCollection.Insert módszerrel beszúrhatja a húzott elem klónját a gyűjteménybe a ListView.Items tárolt beszúrási indexben.
Ezzel a ListView.ListViewItemCollection.Remove módszerrel eltávolíthatja a húzott elem eredeti példányát.
Az eredeti másolat eltávolítása előtt be kell szúrnia a húzott elem klónját, hogy a ListView.Items gyűjtemény indexértékei ne változnak a beszúrás előtt.
Annak biztosításához, hogy az elemek ugyanabban a sorrendben jelenjenek meg, mint az indexértékek, a tulajdonságot a felület olyan implementációjára kell beállítania ListView.ListViewItemSorter , amely indexérték alapján rendezi az IComparer elemeket. További információkért lásd a Példa szakaszt.
A tulajdonság használatával módosíthatja a beszúrási jel színét Color . Ha a beszúrási jel méretére vagy helyzetére van szüksége, a tulajdonságon keresztül lekérheti annak Bounds határoló téglalapját.
Note
A beszúrási jel funkció csak Windows XP és a 2003-Windows Server családon érhető el, amikor az alkalmazás meghívja a Application.EnableVisualStyles metódust. A korábbi operációs rendszereken a program figyelmen kívül hagyja a beszúrási jelhez kapcsolódó kódot, és a beszúrási jel nem jelenik meg. Ennek eredményeképpen előfordulhat, hogy a beszúrási jel funkciótól függő kódok nem működnek megfelelően. Érdemes lehet olyan tesztet is használnia, amely meghatározza, hogy elérhető-e a beszúrási jel funkció, és alternatív funkciókat biztosít, ha az nem érhető el. Előfordulhat például, hogy megkerüli az összes olyan kódot, amely áthúzásos elemek áthelyezését valósítja meg, amikor olyan operációs rendszereken fut, amelyek nem támogatják a beszúrási jeleket.
A beszúrási jel funkciót ugyanaz a kódtár biztosítja, amely az operációs rendszer témáinak funkcióját biztosítja. A kódtár rendelkezésre állásának ellenőrzéséhez hívja meg a metódus túlterhelését FeatureSupport.IsPresent(Object) , és adja meg az OSFeature.Themes értéket.
Tulajdonságok
| Név | Description |
|---|---|
| AppearsAfterItem |
Lekéri vagy beállít egy értéket, amely jelzi, hogy a beszúrási jel megjelenik-e az elem jobb oldalán a tulajdonság által Index megadott indexkel. |
| Bounds |
Lekéri a beszúrási jel határoló téglalapját. |
| Color |
Lekéri vagy beállítja a beszúrási jel színét. |
| Index |
Lekéri vagy beállítja annak az elemnek az indexét, amely mellett megjelenik a beszúrási jel. |
Metódusok
| Név | Description |
|---|---|
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| NearestIndex(Point) |
A megadott ponthoz legközelebb eső elem indexét kéri le. |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |