得到拖过来的路径的代码:(System.Array)e.Data.GetData(DataFormats.FileDrop)
然后你可以根据这些路径复制粘贴了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinToXml
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
// Winform窗体文本框实现拖拽获得目录或文件路径(C#)
protected void SetAllTextBox(Control org)
{
foreach (Control txt in org.Controls)
{
if (txt is TextBox)
{
txt.AllowDrop = true;
txt.DragDrop += new DragEventHandler(txt_ObjDragDrop);
txt.DragEnter += new DragEventHandler(txt_ObjDragEnter);
}
else
{
if (txt.Controls.Count > 0)
{
SetAllTextBox(txt);
}
}
}
}
private void txt_ObjDragEnter( object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Link; // 拖动时的图标
}
private void txt_ObjDragDrop( object sender, DragEventArgs e)
{
((TextBox)sender).Text
= ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue( 0).ToString();
}
private void FrmMain_Load( object sender, EventArgs e)
{
SetAllTextBox( this);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinToXml
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
// Winform窗体文本框实现拖拽获得目录或文件路径(C#)
protected void SetAllTextBox(Control org)
{
foreach (Control txt in org.Controls)
{
if (txt is TextBox)
{
txt.AllowDrop = true;
txt.DragDrop += new DragEventHandler(txt_ObjDragDrop);
txt.DragEnter += new DragEventHandler(txt_ObjDragEnter);
}
else
{
if (txt.Controls.Count > 0)
{
SetAllTextBox(txt);
}
}
}
}
private void txt_ObjDragEnter( object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Link; // 拖动时的图标
}
private void txt_ObjDragDrop( object sender, DragEventArgs e)
{
((TextBox)sender).Text
= ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue( 0).ToString();
}
private void FrmMain_Load( object sender, EventArgs e)
{
SetAllTextBox( this);
}
}
}
在应用程序中,是通过处理一系列事件,如DragEnter,DragLeave和DragDrop事件来实现在Windows应用程序中的拖放操作的。通过使用这些事件参数中的可用信息,可以轻松实现拖放操作。