将文件url从winforms控件拖到外部应用程序 [英] Drag a file url from a winforms control to external application
问题描述
我需要从datagridview拖动表示文件的行,并且能够将它们拖放到任何Windows应用程序,就像我正在从Windows资源管理器中拖动一样,我对于遵循的过程有些困惑。
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.
推荐答案
我终于明白了。程序如下:
I finally figured it out. The procedure is as follows :
- 创建一个包含文件路径数组的DataObject。
-
- 将DataObject传递到DoDragDrop过程
示例代码:
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);
}
这篇关于将文件url从winforms控件拖到外部应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!