C#中如何实现数据拖动?

来源:百度知道 编辑:UC知道 时间:2024/09/13 03:11:42
就像那中文件夹可以拖动到另外的文件夹中一样,数据可以实现自由拖动
网页中如何实现 不是winform程序

首先要把窗口(控件也行 如panel)的AllowDrop属性改成true
然后重写窗口的OnDragEnter事件和OnDragDrop事件
//这个方法用于改变拖动时的鼠标样式 而且是必须有的
//否则不会触发OnDragDrop事件
//DataFormats.FileDrop表示拖动的类型是文件
protected override void OnDragEnter(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
base.OnDragEnter(e);
}
protected override void OnDragDrop(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
try
{
//第一个文件名 可以拖动多个文件
string fileName = ((String[])e.Data.GetData(DataFormats.FileDrop))[0];
//做其它事情....
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
DragDrop(this, e);
}

你是网页还是应用程序啊 这个不一样的
网页的话 比较麻烦了 推荐用flash或者脚本了
应用程序的话