Drag a file url from a winforms control to external application

南笙酒味 提交于 2019-12-13 02:02:26

问题


i need to drag rows representing files from a datagridview, and be able to drop them to any windows application as if i was doing a drag from the windows explorer, and i'm a little bit confused regarding the procedure to follow.

For the moment i have the following event handler

private void gridFiles_MouseDown(object sender, MouseEventArgs e)
{
    gridFiles.DoDragDrop(gridFiles.SelectedRows.Count, DragDropEffects.Move);
}

The thing is that i also need to be able to drop the rows into a control inside my application, and that in this case i would like to be able to get application specific information about the rows.


回答1:


I finally figured it out. The procedure is as follows :

  • Create a DataObject which will contain an array of file paths.
  • Set the DataObject type to FileDrop
  • Pass the DataObject to the DoDragDrop procedure

Sample code :

if (is_in_selection)
{
    sel_rows = from DataGridViewRow r in gridFiles.SelectedRows select r;
    var files = (from DataGridViewRow r in gridFiles.SelectedRows select all_files[r.Index]);
    string[] files_paths = files.Select((f) => f.FullPathName).ToArray();
    var data = new DataObject(DataFormats.FileDrop, files_paths);
    gridFiles.DoDragDrop(data, DragDropEffects.Copy);
}


来源:https://stackoverflow.com/questions/6494400/drag-a-file-url-from-a-winforms-control-to-external-application

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