我来给你,刚写的 /// <summary> /// Begins when user want to drag an item /// </summary> /// <param name="sender"></param> /// <param name="e"></param> int CtrlMask = 8; private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e) { if(e.Button == MouseButtons.Left) { //invoke the drag and drop operation DoDragDrop(e.Item, DragDropEffects.Move | DragDropEffects.Copy); } }/// <summary> /// The mouse drags an item into the client area on this control /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { // Check to be sure that the drag content is the correct type for this // control. if not, reject the drop. if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode")) { // if the Ctrl key was pressed during the drag operation then perform // a Copy. if not, perform a Move. if ((e.KeyState & CtrlMask) == CtrlMask) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.Move; } } else { e.Effect = DragDropEffects.None; } }private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { // Initialize variable that holds the node dragged by the user. TreeNode OriginationNode = (TreeNode) e.Data.GetData("System.Windows.Forms.TreeNode");
// Calling GetDataPresent is a little different for a TreeView than for an // image or text because a TreeNode is not a member of the DataFormats // class. That is, it's not a predefined type. such, you need to use a // different overload, one that takes the type a string.
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { // Use PointToClient to compute the location of the mouse over the Point pt = ((TreeView) sender).PointToClient(new Point(e.X, e.Y)); // Use this Point to get the closest node in the destination TreeView. TreeNode DestinationNode = ((TreeView) sender).GetNodeAt(pt); // The if statement ensures that the user doesn't completely lose the // node if they accidentally release the mouse button over the node they // attempted to drag. Without a check to see if the original node is the // same the destination node, they could make the node disappear.
if (DestinationNode.TreeView == OriginationNode.TreeView) {DestinationNode.Nodes.Add((TreeNode) OriginationNode.Clone());
// Expand the parent node when adding the new node so that the drop // is obvious. Without this, only a + symbol would appear. DestinationNode.Expand(); // if the Ctrl key was not pressed, remove the original node to // effect a drag-and-drop move. if ((e.KeyState & CtrlMask) != CtrlMask) { OriginationNode.Remove(); }} } }
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("A");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("B");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("C");
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// listView1
//
this.listView1.AllowDrop = true;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1});
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
listViewItem1.Tag = "A";
listViewItem2.Tag = "B";
listViewItem3.Tag = "C";
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3});
this.listView1.Location = new System.Drawing.Point(24, 24);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(152, 296);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listView1_DragDrop);
this.listView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listView1_DragEnter);
this.listView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.listView1_ItemDrag);
//
// columnHeader1
//
this.columnHeader1.Width = 115;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(208, 461);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
ListViewItem lv =e.Data.GetData(typeof(System.Windows.Forms.ListViewItem)) as ListViewItem;
ListViewItem l =lv.Clone() as ListViewItem;
Point p =new Point(e.X,e.Y);
p=this.listView1.PointToClient(p);
ListViewItem tempItem= this.listView1.GetItemAt(p.X,p.Y);
int index =-1;
if(tempItem!=null)
{
index =tempItem.Index;
}
lv.Remove();
if(index>-1)
{
this.listView1.Items.Insert(index,l);
}
else
{
this.listView1.Items.Add(l);
}
} private void listView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item,DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.Windows.Forms.ListViewItem)))
{
e.Effect =DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None ;
}
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
寸寸青丝愁华年
对月形单望相护
只羡鸳鸯不羡仙
/// <summary>
/// Begins when user want to drag an item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
int CtrlMask = 8;
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//invoke the drag and drop operation
DoDragDrop(e.Item, DragDropEffects.Move | DragDropEffects.Copy);
}
}/// <summary>
/// The mouse drags an item into the client area on this control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
// Check to be sure that the drag content is the correct type for this
// control. if not, reject the drop.
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
{
// if the Ctrl key was pressed during the drag operation then perform
// a Copy. if not, perform a Move. if ((e.KeyState & CtrlMask) == CtrlMask)
{
e.Effect = DragDropEffects.Copy;
}
else {
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
// Initialize variable that holds the node dragged by the user.
TreeNode OriginationNode = (TreeNode) e.Data.GetData("System.Windows.Forms.TreeNode");
// Calling GetDataPresent is a little different for a TreeView than for an
// image or text because a TreeNode is not a member of the DataFormats
// class. That is, it's not a predefined type. such, you need to use a
// different overload, one that takes the type a string.
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
// Use PointToClient to compute the location of the mouse over the
Point pt = ((TreeView) sender).PointToClient(new Point(e.X, e.Y)); // Use this Point to get the closest node in the destination TreeView.
TreeNode DestinationNode = ((TreeView) sender).GetNodeAt(pt); // The if statement ensures that the user doesn't completely lose the
// node if they accidentally release the mouse button over the node they
// attempted to drag. Without a check to see if the original node is the
// same the destination node, they could make the node disappear.
if (DestinationNode.TreeView == OriginationNode.TreeView)
{DestinationNode.Nodes.Add((TreeNode) OriginationNode.Clone());
// Expand the parent node when adding the new node so that the drop
// is obvious. Without this, only a + symbol would appear.
DestinationNode.Expand(); // if the Ctrl key was not pressed, remove the original node to
// effect a drag-and-drop move.
if ((e.KeyState & CtrlMask) != CtrlMask)
{
OriginationNode.Remove();
}}
}
}