拖动文件OUT应用程序 [英] Drag file OUT of application

查看:55
本文介绍了拖动文件OUT应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于如何在C#应用程序中接收拖放事件到控件中的示例。但是,我需要通过拖放操作发送我的应用程序的数据。



具体来说,我有一个列出一组文件的DataGridView。这些文件可以是任何东西 - Excel,Word,BMP,你可以命名它,我的应用程序允许用户导入它,并查看已导入的列表。



我需要用户能够将文件从我的DataGridView拖到另一个(外部)应用程序中。我有所有基本代码来执行拖放操作(现在它将文件路径作为字符串发送),但我无法弄清楚如何将实际文件数据包装到正确的对象表单中DoDragDrop事件。



目前,我在拖放启动时将所选文件写入TMP文件夹,因此引用是指磁盘上的实际文件。但是,我对存储在数据库中的每个文件都有二进制文件,因此如果解决方案直接从数据库获取数据,则完全可以接受。

There are tons of examples about how to receive a drag/drop event into a control in a C# application. However, I need to send data OUT of my application via drag/drop.

Specifically, I have a DataGridView that lists a set of files. The files can be anything - Excel, Word, BMP, you name it and my application allows the user to import it, and see a list of what''s been imported.

I need for the user to be able to drag a file out of my DataGridView and into another (external) application. I have all the basic code in place to do the drag/drop (right now it is sending a file path as a String), but I can''t figure out how to wrap the actual file data into the correct object form for the DoDragDrop event.

Currently, I write the selected files to a TMP folder on drag/drop initiation, so the reference is to an actual file on disk. However, I have the binary for each file stored in a database, so it is perfectly acceptable if the solution takes data directly from the database.

推荐答案

我认为部分回答是使用clipbord。

使用.NET处理剪贴板 [ ^ ]



我根本不会使用拖拽程序,因为我认为这会让人更加困惑。
I think the partial answer would be to use the clipbord.
Clipboard handling with .NET[^]

I woudnt use the dragdrop routine at all though, as I think it would be more confusing to people.


我''我不确定拖放会如何混淆。在任何情况下,这都是我必须编码的要求。



经过深思熟虑和研究后,我偶然发现了一个想法:我继续实施标准拖动/ drop to我的DataGridView,并通过应用程序调试以查看提交的数据类型。这使我进入了OleDataObject类,它可以简单地实现为System.Windows.Forms.DataObject。现在,代码的核心(我已经简化了插图)变成了......



I''m not sure how drag/drop would be confusing. In any case, that is the requirement that I must code to.

After much thought and research, I stumbled upon an idea: I went ahead and implemented standard drag/drop TO my DataGridView, and debugged through the app to see what kind of data was being submitted. This led me to the OleDataObject class, which can be implemented simply as System.Windows.Forms.DataObject. Now, the core of the code (which I''ve simplified for illustration) becomes...

// You may not need all these USING statements, but I did not take time to filter out only the ones related to this example.
				
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

	String strTmpFileName = Path.GetTempFileName();// Get a temp file to write to
						strTmpFileName = Path.ChangeExtension(strTmpFileName, doc.DocType);// Change file extension from "TMP" to appropriate type so that the appropriate file association will be found
						DataObject dragObject = new System.Windows.Forms.DataObject();
						StringCollection filePaths = new StringCollection();
						filePaths.Add(strTmpFileName);
						dragObject.SetFileDropList(filePaths);





此代码将文件路径设置为拖动对象,然后可以将其传递给DoDragDrop函数。在执行拖动操作之前,请确保该文件存在。我通过在用户选择DataGridView中的行时编写TMP文件来执行此操作。



This code sets a filepath into the drag object, which can then be passed to the DoDragDrop function. Make sure the file exists before a drag operation takes place. I do this by writing a TMP file as soon as the user selects a row in my DataGridView.


这篇关于拖动文件OUT应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆