ListViewInsertionMark Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Slouží k označení očekávaného umístění přetažení, když je položka přetažena na nové místo v ovládacím ListView prvku. Tato funkce je dostupná pouze v systému Windows XP a novějších verzích.
public ref class ListViewInsertionMark sealed
public sealed class ListViewInsertionMark
type ListViewInsertionMark = class
Public NotInheritable Class ListViewInsertionMark
- Dědičnost
-
ListViewInsertionMark
Příklady
Následující příklad kódu ukazuje, jak použít ListView funkci značky vložení a implementovat přetahování položek přeuspořádání pomocí standardních událostí přetažení. Pozice značky vložení se aktualizuje v obslužné rutině Control.DragOver události. V této obslužné rutině se pozice ukazatele myši porovná s prostředním bodem nejbližší položky a výsledek slouží k určení, zda se značka vložení zobrazuje vlevo nebo vpravo od položky.
#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
Poznámky
Můžete načíst ListViewInsertionMark z InsertionMark vlastnosti ListView ovládacího prvku a použít ho k vizuálnímu označení očekávaného umístění přetažení v operaci přetažení, když je položka přetažena na nové místo.
Tato funkce funguje pouze v případě, ListView.AutoArrange že je vlastnost nastavena true a pokud ListView ovládací prvek neřadí položky automaticky. Chcete-li zabránit automatickému řazení, ListView.Sorting musí být vlastnost nastavena SortOrder.None a ListView.View vlastnost musí být nastavena na View.LargeIconhodnotu , View.SmallIconnebo View.Tile. Kromě toho nelze funkci značky vložení použít s ListView funkcí seskupení, protože funkce seskupení objednává položky podle členství ve skupině.
ListViewInsertionMark Třída se obvykle používá v obslužné rutině nebo Control.DragOverControl.MouseMove události k aktualizaci pozice značky vložení při přetahování položky. Používá se také v obslužné rutině Control.DragDrop nebo Control.MouseUp události k vložení přetažené položky do správného umístění.
Pokud chcete aktualizovat pozici značky vložení, postupujte takto:
V obslužné rutině Control.DragOver nebo Control.MouseMove události použijte ListView.InsertionMark vlastnost pro přístup k objektu ListViewInsertionMark přidruženému k ListView ovládacího prvku.
NearestIndex Pomocí metody načtěte index položky nejblíže ukazateli myši.
Předejte hodnotu indexu ListView.GetItemRect metodě pro načtení ohraničující obdélník položky.
Pokud je ukazatel myši umístěn vlevo od středu ohraničujícího obdélníku, nastavte AppearsAfterItem vlastnost na
false; jinak jej nastavte natrue.Index Nastavte vlastnost na hodnotu indexu NearestIndex načtenou z metody. V závislosti na AppearsAfterItem hodnotě vlastnosti se zobrazí vedle položky se zadaným indexem( vlevo nebo vpravo). Pokud je položka přetažena nad sebou, index se -1 a značka vložení je skrytá.
Chcete-li vložit přetaženou položku do správného umístění, postupujte takto:
V obslužné rutině nebo Control.DragDropControl.MouseUp události použijte Index vlastnost k určení aktuálního umístění značky vložení. Uložte tuto hodnotu, která se použije později jako index vložení.
Pokud je vlastnost nastavena AppearsAfterItem na
true, zvýšit uloženou hodnotu indexu vložení.ListView.ListViewItemCollection.Insert Pomocí metody vložte klon přetažené položky do ListView.Items kolekce do uloženého indexu vložení.
Použijte metodu ListView.ListViewItemCollection.Remove k odebrání původní kopie přetažené položky.
Před odebráním původní kopie je nutné vložit klon přetažené položky, aby se před vložením nezměnily hodnoty indexu v ListView.Items kolekci.
Chcete-li zajistit, aby se položky zobrazovaly ve stejném pořadí jako jejich indexové hodnoty, je nutné nastavit ListView.ListViewItemSorter vlastnost na implementaci IComparer rozhraní, které seřadí položky podle hodnoty indexu. Další informace najdete v části Příklad.
Barvu značky vložení můžete upravit pomocí Color vlastnosti. Pokud potřebujete velikost nebo pozici značky vložení, můžete přes vlastnost získat jeho ohraničující obdélník Bounds .
Poznámka:
Funkce značky vložení je k dispozici pouze v systémech Windows XP a řady Windows Server 2003, když aplikace volá metodu Application.EnableVisualStyles . V dřívějších operačních systémech se veškerý kód týkající se značky vložení bude ignorovat a značka vložení se nezobrazí. V důsledku toho nemusí jakýkoli kód, který závisí na funkci značky vložení, fungovat správně. Můžete zahrnout test, který určuje, jestli je funkce značky vložení k dispozici, a poskytnout alternativní funkce, pokud není k dispozici. Můžete například vynechat veškerý kód, který implementuje přemísťování položek přetažením při spuštění v operačních systémech, které nepodporují značky vložení.
Funkce značky vložení je poskytována stejnou knihovnou, která poskytuje funkci motivů operačního systému. Chcete-li zkontrolovat dostupnost této knihovny, zavolejte FeatureSupport.IsPresent(Object) přetížení metody a předejte OSFeature.Themes hodnotu.
Vlastnosti
| Name | Description |
|---|---|
| AppearsAfterItem |
Získá nebo nastaví hodnotu označující, zda se značka vložení zobrazí napravo od položky s indexem určeným Index vlastností. |
| Bounds |
Získá ohraničující obdélník značky vložení. |
| Color |
Získá nebo nastaví barvu značky vložení. |
| Index |
Získá nebo nastaví index položky, vedle které se zobrazí značka vložení. |
Metody
| Name | Description |
|---|---|
| Equals(Object) |
Určuje, zda je zadaný objekt roven aktuálnímu objektu. (Zděděno od Object) |
| GetHashCode() |
Slouží jako výchozí funkce hash. (Zděděno od Object) |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| NearestIndex(Point) |
Načte index položky nejblíže zadanému bodu. |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |