C#中的拖放操作 (转载)

C#中的拖放操作 (转载)

 

由TreeView组件到ListView组件的拖放操作: 

必须处理好三种事件:"ItemDrag"、"DragEnter"、"DragDrop"。其中只有第一种事件是在源组件中触发的,另外二种事件是在目标组件中触发的。其中当用户拖动组件触发"ItemDrag"事件;当拖动数据进入目标组件区域触发"DragEnter"事件;当用户在目标组件区域放置拖动的数据触发"DragDrop"事件。下面就根据拖放操作的操作顺序来详细介绍:

(1).开始"拖"(Drag)操作:
 通过"DoDragDrop"方法拉开了拖放操作的第一步。"DoDragDrop"方法的语法为: DoDragDrop ( object data , DragDropEffects allowedEffects ) ;
其中第二个参数来是说明此次拖放操作最后所要实现的效果,因为拖放操作有时实现的效果是把源组件中的内容"拖"到目标组件中,这种效果就是 "Move";有时拖放的效果是在目标组件中加入拖动的数据,对源组件的内容是没有什么影响的,这种效果就是"Copy"。当然无论是"Move"还是 "Copy",这都要通过具体的编程来实现,设定这些效果只是告诉操作系统,你进行拖放操作的类型,从而为拖放操作设定特定的图标。此例中实现开始"拖放 "操作的具体实现代码如下: 

private   void  treeView1_ItemDrag (  object  sender , ItemDragEventArgs e )
{
string strItem = e.Item.ToString ( ) ; 
//开始进行"Drag"操作
DoDragDrop ( strItem , DragDropEffects.Copy | DragDropEffects.Move ) ;
}



在上面代码中,我们定义的拖放数据类型是字符串,其实拖放的数据类型可以是很多种的,你可以通过修改"DoDragDrop"方法的第一个参数来设定你所要拖放数据类型,譬如:位图或者其他什么。

(2).目标组件允许进行拖放操作:

既然你已经开始进行拖放操作,你还必须告诉你要拖放到的目标组件,要接受你所拖放的数据,"DragEnter"事件正好可以处理。在下列的代码中,我们是通过判断拖放数据类型来确定是否接受拖放,如果是字符串,则可以,否则,则不行。具体代码如下:

private   void  listView1_DragEnter (  object  sender , DragEventArgs e )
{
//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝
if ( e.Data.GetDataPresent ( DataFormats.Text ) )
e.Effect 
= DragDropEffects.Move ;
else
e.Effect 
= DragDropEffects.None ;
}


(3).获得拖放的字符串,在目标组件中加入相应的内容:

此步的处理过程是十分明确的,要分成二步来进行,首先要得到拖放的字符串,其次是在目标组件中加入以此字符串为标题的项目。当然还要在相应的位置了。下面就是实现这二步操作的具体代码:

 

private   void  listView1_DragDrop (  object  sender , DragEventArgs e )
{
string dummy = "temp" ;
//获得进行"Drag"操作中拖动的字符串
string s = ( string ) e.Data.GetData ( dummy.GetType ( ) ) ;
= s.Substring ( s.IndexOf ( ":" ) + 1 ).Trim ( ) ;
Position.X 
= e.X ;
Position.Y 
= e.Y ;
Position 
= listView1.PointToClient ( Position ) ;
//在目标组件中加入以此字符串为标题的项目
listView1.Items.Add ( new ListViewItem ( s , 0 ) ) ;
}
 

此致通过对这三个事件的编程,已经完成了由TreeView组件到ListView组件的拖放操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
using System; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; namespace WindowsFormsApplication1 { class DragItemListView:ListView { public DragItemListView() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); this.MultiSelect = false; this.ListViewItemSorter = new ListViewIndexComparer(); if (OSFeature.Feature.IsPresent(OSFeature.Themes)) { this.AllowDrop = true; this.ItemDrag += new ItemDragEventHandler(DragItemListView_ItemDrag); this.DragEnter += new DragEventHandler(DragItemListView_DragEnter); this.DragLeave += new EventHandler(DragItemListView_DragLeave); this.DragOver += new DragEventHandler(DragItemListView_DragOver); this.DragDrop += new DragEventHandler(DragItemListView_DragDrop); } } void DragItemListView_DragDrop(object sender, DragEventArgs e) { // Retrieve the index of the insertion mark; int targetIndex = this.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. // 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. ListViewItem NewItem = (ListViewItem)draggedItem.Clone(); if (draggedItem.Index < targetIndex) { if (targetIndex - draggedItem.Index > 1) NewItem.Group = this.Items[targetIndex - 1].Group; else NewItem.Group = this.Items[targetIndex].Group; } else { if (draggedItem.Index - targetIndex > 1) NewItem.Group = this.Items[targetIndex].Group; else NewItem.Group = this.Items[targetIndex - 1].Group; } if (this.InsertionMark.AppearsAfterItem) { targetIndex++; } this.Items.Insert(targetIndex, NewItem); // Remove the original copy of the dragged item. this.Items.Remove(draggedItem); } void DragItemListView_DragOver(object sender, DragEventArgs e) { // Retrieve the client coordinates of the mouse pointer. Point targetPoint = this.PointToClient(new Point(e.X, e.Y)); // Retrieve the index of the item closest to the mouse pointer. //int targetIndex = this.InsertionMark.NearestIndex(targetPoint); int targetIndex = GetNearItem(targetPoint).Index; // 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 = this.GetItemRect(targetIndex); if (targetPoint.X > itemBounds.Left + (itemBounds.Width / 2)) { this.InsertionMark.AppearsAfterItem = true; } else { this.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. this.InsertionMark.Index = targetIndex; } void DragItemListView_DragLeave(object sender, EventArgs e) { this.InsertionMark.Index = -1; } void DragItemListView_ItemDrag(object sender, ItemDragEventArgs e) { this.DoDragDrop(e.Item, DragDropEffects.Move); } void DragItemListView_DragEnter(object sender, DragEventArgs e) { e.Effect = e.AllowedEffect; } private class ListViewIndexComparer : System.Collections.IComparer { public int Compare(object x, object y) { return ((ListViewItem)x).Index - ((ListViewItem)y).Index; } } /// <summary> /// 搜索最近的ListViewItem /// </summary> /// <param name="ClientPoint">工作区坐标</param> /// <returns></returns> public ListViewItem GetNearItem(Point ClientPoint) { ListViewItem lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y); if (lvt != null) return lvt; List<ListViewItem> vt = new List<ListViewItem>(); for (int i = 1; i < 10; i++) { lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X + i, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y + i); if (lvt != null) vt.Add(lvt); lvt = this.GetItemAt(ClientPoint.X - i, ClientPoint.Y - i); if (lvt != null) vt.Add(lvt); if (vt.Count > 0) break; } if (vt.Count == 0) { return null; } else if (vt.Count == 1) { return vt[0]; } else { double HisDis = 0; int i = 0; foreach (ListViewItem item in vt) { double dis = GetDistince(ClientPoint, item.Position); if (i == 0 || dis < HisDis) { lvt = item; HisDis = dis; } i++; } return lvt; } } /// <summary> /// 两点间的距离 /// </summary> /// <param name="start">起点</param> /// <param name="end">终点</param> /// <returns></returns> private double GetDistince(Point start, Point end) { double distince2 = Math.Pow((start.X - end.X), 2) + Math.Pow((start.Y - end.Y), 2); return Math.Abs(Math.Sqrt(distince2)); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一天雨水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值