繁体   English   中英

将文件直接拖到窗体上不受控制

[英]Drag Files Directly Onto Form Not Control

是否可以将文件直接拖到Windows窗体上,还是必须将其拖到Windows窗体上的控件上? 我已经使用以下代码已有一段时间了,但这需要您拖到ListView

public Form1()
{
  InitializeComponent();
  this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
  this.listView1.AllowDrop = true;
  this.listView1.Columns.Add("File name");
  this.listView1.Dock = DockStyle.Fill;
  this.listView1.SmallImageList = this.imageList1;
  this.listView1.View = View.Details;
  this.listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
  this.listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
}
void listView1_DragEnter(object sender, DragEventArgs e)
{
  if (e.Data.GetDataPresent("FileDrop") &&
      (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
    e.Effect = DragDropEffects.Copy;
}
}

void listView1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent("FileDrop") &&
    (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        string[] filePaths = (string[])e.Data.GetData("FileDrop");
        ListViewItem[] items = new ListViewItem[filePaths.Length];
        string filePath;
        for (int index = 0; index < filePaths.Length; index++)
        {
          filePath = filePaths[index];
          if (!this.imageList1.Images.Keys.Contains(filePath))
          {
            this.imageList1.Images.Add(filePath,
            Icon.ExtractAssociatedIcon(filePath));
          }
        items[index] = new ListViewItem(filePath, filePath);
    }
      this.listView1.Items.AddRange(items);
  }
}
this.DragDrop += new DragEventHandler(form_DragDrop);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM