Implement file dragging to the desktop from a .net winforms application?

旧街凉风 提交于 2019-12-31 10:32:33

问题


I have a list of files with their names in a listbox and their contents stored in an SQL table and want the user of my app to be able to select one or more of the filenames in the listbox and drag them to the desktop, yielding the actual files on the desktop. I can't find any documentation on how to do this. Can anyone explain or point to an explanation?

Added later: I've been able to make this work by handling the DragLeave event. In it I create a file in a temporary directory with the selected name and the contents pulled from SQL Server. I then put the path to the file into the object:

var files = new string[1];
files[0] = "full path to temporary file";
var dob = new DataObject();    
dob.SetData(DataFormats.FileDrop, files);
DoDragDrop(dob, DragDropEffects.Copy);

But this seems very inefficient and clumsy, and I have not yet figured out a good way to get rid of accumulated temp files.


回答1:


I can help you somewhat. Here's some code that will allow you to drag something out of the listbox, and when dropped on the desktop, it will create a copy of a file that exists on your machine to the desktop.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.listBox1.Items.Add("foo.txt");
        this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
        this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);
    }

    void listBox1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        string[] filesToDrag = 
        {
            "c:/foo.txt"
        };
        this.listBox1.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
    }
}



回答2:


Here's some of the boiler plate to help you determine when to start a drag-operation:

private Rectangle _DragRect;

private void MyDragSource_MouseDown(object sender, MouseEventArgs e) {
   Size dragsize = SystemInformation.DragSize;
   _DragRect = new Rectangle(new Point(e.X - (dragsize.Width / 2), e.Y - (dragsize.Height / 2)), dragsize);
}

private void MyDragSource_MouseMove(object sender, MouseEventArgs e) {
   if (e.Button == MouseButtons.Left) {
      if (_DragRect != Rectangle.Empty && !_DragRect.Contains(e.X, e.Y)) { 
         // the mouse has moved outside of the drag-rectangle.  Start drag operation

         MyDragSource.DoDragDrop(.....)
      }
   }
}

private void MyDragSource_MouseUp(object sender, MouseEventArgs e) {
   _DragRect = Rectangle.Empty; // reset
}



回答3:


I found a better solution by extending System.Windows.Forms.DataObject
Transferring Virtual Files to Windows Explorer in C#

also found some threads here on StackOverFlow that may help
Drag and drop large virtual files from C# to Windows Explorer



来源:https://stackoverflow.com/questions/1008984/implement-file-dragging-to-the-desktop-from-a-net-winforms-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!