从.net winforms应用程序实现文件拖动到桌面?

时间:2022-07-09 15:21:44

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?

我有一个文件列表,其列表框中包含其名称,其内容存储在SQL表中,并希望我的应用程序的用户能够在列表框中选择一个或多个文件名并将其拖到桌面上,从而产生桌面上的实际文件。我找不到有关如何执行此操作的任何文档。谁能解释或指出解释?

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:

稍后添加:我已经能够通过处理DragLeave事件来完成这项工作。在其中,我在临时目录中创建一个文件,其中包含所选名称和从SQL Server中提取的内容。然后我将文件的路径放入对象:

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.

但这似乎非常低效和笨拙,我还没有想出一个摆脱累积的临时文件的好方法。

3 个解决方案

#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#

我通过扩展System.Windows.Forms.DataObject在C#中将虚拟文件传输到Windows资源管理器找到了更好的解决方案

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

还在*上找到了一些线程,可以帮助将大型虚拟文件从C#拖放到Windows资源管理器中

#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#

我通过扩展System.Windows.Forms.DataObject在C#中将虚拟文件传输到Windows资源管理器找到了更好的解决方案

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

还在*上找到了一些线程,可以帮助将大型虚拟文件从C#拖放到Windows资源管理器中