Drag and Drop in C#?

回眸只為那壹抹淺笑 提交于 2019-12-21 05:22:07

问题


How to Implement Drag and Drop Between my Program and Explorer ya windows application only


回答1:


As long as you're using WinForms, it's actually very straightforward. See these two articles to get you started:

  • Drag and Drop files from Windows Explorer to Windows Form
  • Drag and Drop Text Files from Windows Explorer to your Windows Form Application

And just in case you're using WPF, this tutorial and this SO thread should help.




回答2:


There is a good article on CodeProject about how to do this:

This sample project lists a folder full of files, and lets you drag and drop them into Explorer. You can also drag from Explorer into the sample, and you can use the Shift and Ctrl keys to modify the action, just like in Explorer.

Drag and drop, cut/copy and paste files with Windows Explorer

To start a drag operation into Explorer, we implement the ItemDrag event from the Listview, which gets called after you drag an item more than a few pixels. We simply call DoDragDrop passing the files to be dragged wrapped in a DataObject. You don't really need to understand DataObject - it implements the IDataObject interface used in the communication.




回答3:


Add this on the Drag enter event (this will change the cursor type when you are dragging a file)

 private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        // If file is dragged, show cursor "Drop allowed"
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }

Then on the DragDrop event you need to handle what do ou want to do. And also set the AllowDrop property to true



来源:https://stackoverflow.com/questions/669204/drag-and-drop-in-c

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