簡體   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