全面掌握C#中的拖放操作

全面掌握C#中的拖放操作

 

随着桌面系的推出,利用鼠的拖放(Drag and Drop)操作由于其简单、直接,受到了越来越多的者的迎,迎合这种趋势,越来越多程序在自己的程序中使用了拖放操作。拖放操作方便了程序的使用者,但由于拖放操作在程序中的设计工作比较还有点麻,甚至是一个点,多程序员对其都有点心有余悸。本文就合微公司最新的.Net程序开发语言–C#,来全面介一下在C#中是如何理拖放操作的。 
 

在本文中,我是通二个代表件,也是在拖放操作中常使用到的二个件–TreeView件和ListView件,之互相行拖放操作来明此类问题的。在行拖放操作之前,必对进行拖放操作的件的”AllowDrop”属性值设“True”,因此属性是确定件是否可以行拖放操作的。 

一. 本文中介的程序的设计和运行的境: 

(1).微公司窗2000服器版 

(2)..Net FrameWork SDK Beta 2 

二. 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 = 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
件的拖放操作。 

三. ListView件到TreeView件的拖放操作: 

由ListView件到TreeView件的拖放操作和从TreeView件到ListView件相似,也是通“ItemDrag”、”DragEnter”、”DragDrop”三个事件来理的,具体如下: 

(1).始”拖”(Drag)操作: 

和前者没有什么实质上的区,只是在此次的拖放操作始之前,多加入了一些逻辑判断,程序更健的允实现的代如下:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)

{

//判断是否是鼠标右键按动

if (e.Button == MouseButtons.Right)

return;

int nTotalSelected = listView1.SelectedIndices.Count;

//判断组件中是否存在项目

if (nTotalSelected <= 0)

return;

IEnumerator selCol = listView1.SelectedItems.GetEnumerator();

selCol.MoveNext();

ListViewItem lvi = (ListViewItem) selCol.Current;

string mDir = “”;

for (int i = 0; i < lvi.SubItems.Count; i++)

mDir += lvi.SubItems[i].Text + ” ,”;

string str = mDir.Substring(0, mDir.Length – 1);

if (str == “”)

return;

//对组件中的字符串开始拖放操作

listView1.DoDragDrop(str, DragDropEffects.Copy | DragDropEffects.Move);

}


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

行拖放操作最一致的,除非你所要行拖放的数据型有改,否,没有必要源代码进行什修改,具体如下:

private void treeView1_DragEnter(object sender, DragEventArgs e)

{

//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝

if (e.Data.GetDataPresent(DataFormats.Text))

e.Effect = DragDropEffects.Copy;

else

e.Effect = DragDropEffects.None;

}


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

行拖放操作的不同件,得其拖放的数据的实现方法是不一的,在本步骤中也不例外,但总归大同小异,掌握程序设计步骤和要点,加上探索、研究的精神,问题应该解决,下面是实现步骤的程序代

private void treeView1_DragDrop(object sender, DragEventArgs e)

{

//获得进行“Drag”操作中拖动的字符串

string dummy = “temp”;

string s = (string) e.Data.GetData(dummy.GetType());

s = s.Substring(s.IndexOf(“:”) + 1).Trim();

Position.X = e.X;

Position.Y = e.Y;

Position = treeView1.PointToClient(Position);

TreeNode DropNode = this.treeView1.GetNodeAt(Position);

//在目标组件中加入以此字符串为标题的项目

if (DropNode != null)

{

TreeNode DragNode = new TreeNode(s);

treeView1.Nodes.Insert(DropNode.Index + 1, DragNode);

}

}


四. 二个
行拖放操作的完整源程序代(dragdrop.cs): 

在掌握了上面的步骤过以后,可以得到二个件相互行拖放操作的完整代编译后程序的运行界面,如下: 

 

 

01:二个件相互行拖放的程序运行界面 

dragdrop.cs的代如下:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

//导入程序中使用的命名空间

public class Form1 : Form

{

private TreeView treeView1;

private Point Position = new Point(0, 0);

// bool lv1_mdown = false ;

private ListView listView1;

private System.ComponentModel.Container components = null;

public Form1()

{

InitializeComponent();

//初始化窗体中的各个组件

}

//清除程序中使用到的各种资源

protected override void Dispose(bool disposing)

{

if (disposing)

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

private void InitializeComponent()

{

ListViewItem listViewItem1 = new ListViewItem(“Item01”);

ListViewItem listViewItem2 = new ListViewItem(“Item02”);

treeView1 = new TreeView();

listView1 = new ListView();

SuspendLayout();

//此属性必须设定为“真”,这样才能进行拖放操作

treeView1.AllowDrop = true;

treeView1.ImageIndex = -1;

treeView1.Location = new Point(48, 40);

treeView1.Name = “treeView1”;

//在TreeView组件中加入初始化的节点

treeView1.Nodes.Add(new TreeNode(节点01″));

treeView1.Nodes.Add(new TreeNode(节点02″));

treeView1.SelectedImageIndex = -1;

treeView1.Size = new Size(121, 144);

treeView1.TabIndex = 0;

treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);

treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);

treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);

//此属性必须设定为“真”,这样才能进行拖放操作

listView1.AllowDrop = true;

//在ListView组件中加入项目

listView1.Items.Add(listViewItem1);

listView1.Items.Add(listViewItem2);

listView1.Location = new Point(208, 40);

listView1.Name = “listView1”;

listView1.Size = new Size(121, 144);

listView1.TabIndex = 2;

listView1.View = View.List;

listView1.DragDrop += new DragEventHandler(listView1_DragDrop);

listView1.DragEnter += new DragEventHandler(listView1_DragEnter);

listView1.ItemDrag += new ItemDragEventHandler(listView1_ItemDrag);

 

AutoScaleBaseSize = new Size(6, 14);

ClientSize = new Size(376, 237);

Controls.Add(listView1);

Controls.Add(treeView1);

this.MaximizeBox = false;

this.MinimizeBox = false;

this.Name = “Form1”;

this.Text = “全面掌握C#中的拖放操作”;

this.ResumeLayout(false);

}

static void Main()

{

Application.Run(new Form1());

}

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

{

string strItem = e.Item.ToString();

//开始进行“Drag”操作

DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);

}

private void listView1_DragEnter(object sender, DragEventArgs e)

{

//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝

if (e.Data.GetDataPresent(DataFormats.Text))

e.Effect = DragDropEffects.Move;

else

e.Effect = DragDropEffects.None;

}

private void listView1_DragDrop(object sender, DragEventArgs e)

{

string dummy = “temp”;

//获得进行“Drag”操作中拖动的字符串

string s = (string) e.Data.GetData(dummy.GetType());

s = 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));

}

 

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)

{

//判断是否是鼠标右键按动

if (e.Button == MouseButtons.Right)

return;

int nTotalSelected = listView1.SelectedIndices.Count;

//判断组件中是否存在项目

if (nTotalSelected <= 0)

return;

IEnumerator selCol = listView1.SelectedItems.GetEnumerator();

selCol.MoveNext();

ListViewItem lvi = (ListViewItem) selCol.Current;

string mDir = “”;

for (int i = 0; i < lvi.SubItems.Count; i++)

mDir += lvi.SubItems[i].Text + ” ,”;

string str = mDir.Substring(0, mDir.Length – 1);

if (str == “”)

return;

//对组件中的字符串开始拖放操作

listView1.DoDragDrop(str, DragDropEffects.Copy | DragDropEffects.Move);

}

private void treeView1_DragEnter(object sender, DragEventArgs e)

{

//判断是否目前拖动的数据是字符串,如果是,则拖动符串对目的组件进行拷贝

if (e.Data.GetDataPresent(DataFormats.Text))

e.Effect = DragDropEffects.Copy;

else

e.Effect = DragDropEffects.None;

}

private void treeView1_DragDrop(object sender, DragEventArgs e)

{

//获得进行“Drag”操作中拖动的字符串

string dummy = “temp”;

string s = (string) e.Data.GetData(dummy.GetType());

s = s.Substring(s.IndexOf(“:”) + 1).Trim();

Position.X = e.X;

Position.Y = e.Y;

Position = treeView1.PointToClient(Position);

TreeNode DropNode = this.treeView1.GetNodeAt(Position);

//在目标组件中加入以此字符串为标题的项目

if (DropNode != null)

{

TreeNode DragNode = new TreeNode(s);

treeView1.Nodes.Insert(DropNode.Index + 1, DragNode);

}

}

}

五. 其他件的拖放操作: 

本文TreeView件和ListView件之的拖放操作行了详细的介于其他的可以用于拖放操作的件,很多件的拖放操作的实现方法都和差不多。但也有一些件有一些区,譬如:ListBox件等,在行拖放操作的候,他就没有本文介的”ItemDrag”事件,那么办。我是通一个通的方法来实现的。具体是通“MouseMove”事件和”MouseDown”事件来代替”ItemDrag”事件,其中”MouseMove”事件主要是起到触拖放操作的作用,”MouseDown”事件主要是起着判断此次拖放操作是否已完成的作用。于ListBox件拖放操作的其他步骤也和上面介的二个件没有什太大区。由于篇幅的系ListBox件和其他不存在”ItemDrag”事件的件的拖放操作,里就不一一介了,相信大家能搞定。 

六. 总结 

于大多数件来掌握了”ItemDrag”、”DragEnter”、”DragDrop”三个事件的解决法也就掌握了的拖放操作。当然有一些例外的件,但而言之,拖放操作的实现步骤都是一的,解决的思路也是大致一致的。由于拖放操作的自身的点,于程序尽快掌握是十分必要的,希望本文介的内容能令你意。

 

[引文出处:http://www.chenjiliang.com/Article/View.aspx?ArticleID=1578]