在我的 .NET Windows 窗体上实现从 Chrome 拖放

Implementing drag-drop from Chrome on my .NET Windows form(在我的 .NET Windows 窗体上实现从 Chrome 拖放)
本文介绍了在我的 .NET Windows 窗体上实现从 Chrome 拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Google Chrome 有一个方便的功能,我可以单击下载链接并将其拖到 Windows 资源管理器窗口中,然后放下.删除后,Chrome 会下载文件并显示在我删除的位置.

Google Chrome has a handy feature where I can click a download link and drag it into a Windows Explorer window, and then drop. After dropping, Chrome then downloads the file and it appears where I dropped it.

我希望能够从 Google Chrome 拖放到我的应用程序中,但似乎并不那么简单.我有一个名为 gridFiles 的 DataGridView,以及以下代码:

I would like to be able to drop from Google Chrome into my application, but it seems it isn't so simple. I have a DataGridView called gridFiles, and the following code:

Private Sub gridFiles_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles gridFiles.DragDrop
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        Dim DroppedFiles() As String = e.Data.GetData(DataFormats.FileDrop)
        If Not DroppedFiles Is Nothing Then
            For Each file As String In DroppedFiles
                MsgBox(file)
            Next
        End If
    End If
End Sub

Private Sub gridFiles_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles gridFiles.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If

End Sub

当我从 Windows 资源管理器中将文件拖放到其中时,一切正常,并且每个被删除的文件都会出现一个消息框.但是,当我从 Chrome 中删除时,什么也没有发生.原因是 DroppedFiles 等于 Nothing.似乎 e.Data.GetData 没有返回任何内容.我用 e.Data.GetFormats() 检查了格式,它返回 FileDrop, FileName, FileNameW 为预计任何文件丢失.

When I drop files onto it from Windows Explorer, all works fine and I get a message box for each file that was dropped. However, when I drop from Chrome, nothing happens. The reason for this is that DroppedFiles is equal to Nothing. It seems that e.Data.GetData isn't returning anything. I have checked the formats with e.Data.GetFormats() and it returns FileDrop, FileName, FileNameW as expected with any file drop.

我很确定正在发生的是 Chrome 说它有一些文件以便 DragEnter 起作用,但由于它还没有下载文件,DragDrop无法完成,因此 Chrome 不返回任何文件.我怀疑在 Windows 资源管理器上下文中,Chrome 会以某种方式获取该窗口的路径并稍后将文件复制到那里.

What I am quite sure is happening is that Chrome says it has some files so that the DragEnter functions, but since it hasn't downloaded the file yet, DragDrop cannot be done, so Chrome returns no files. I suspect that in a Windows Explorer context, Chrome somehow gets that window's path and copies the file there itself later.

如何欺骗 Chrome 浏览器进入我的应用程序?我通过以某种方式为 Chrome 提供一个它认为已删除文件的临时文件夹来实现这一点,并且我的应用程序将监视该文件夹中的新文件,并在下载后将它们拉入.我只需要找到一种方法让 Chrome 知道"那个文件夹.

How can I fool Google Chrome into dropping into my application? I see this working by somehow giving Chrome a temporary folder where it thinks it has dropped the file, and my application would monitor that folder for new files and pull them in once they are downloaded. I just need to find a way for Chrome to "know" that folder.

或者,如果我能获得所删除内容的 URL,那也很好.然后我可以用我的程序下载文件.

Alternatively, if I could get the URL of what was dropped, that would be just fine as well. I could then download the file with my program.

非常感谢任何和所有建议.谢谢.

Any and all advice is much appreciated. Thanks.

所以看来,使用常规 URL,我确实得到了正确的拖入 UniformResourceLocator 格式.我看到的行为发生在 Gmail 中的下载链接上.它可能发生在其他地方,但我不确定.当一个 gmail 附件从 Gmail 拖到我的应用程序中时,我会得到一个 FileDrop.

So it seems that with regular URLs, I do get the proper dragged-in UniformResourceLocator format. The behavior I am seeing occurs with the download links in Gmail. It probably happens elsewhere, but I am not sure. When a gmail attachment is dragged from Gmail into my application, I get a FileDrop.

做一些更多的挖掘,似乎Gmail正在使用锚标记的download_url属性.我以前从未听说过.也许这只是他们添加的额外属性?

Doing some more digging, it seems that Gmail is using the download_url attribute of the anchor tag. I have never heard of this before. Perhaps this is just an extra property they have added?

在任何情况下,由于我的应用程序将主要用于电子邮件附件,因此我需要一种方法让幻像 FileDrop 工作,如上所述.我无法使用 Spy++.发生掉落时,它似乎没有显示任何消息.(我也欢迎任何关于这个问题的建议.)

In any case, since my application will primarily be used with e-mail attachments, I need a way for the phantom FileDrop to work, as stated above. I am unable to use Spy++. It doesn't seem to show any messages when drops occur. (I welcome any advice on that problem as well.)

编辑 #2: 以下是有关 Gmail 如何利用拖放操作文件的更多信息:http://www.thecssninja.com/javascript/gmail-dragout

Edit #2: Here is more information on how Gmail utilizes drag/drop for files: http://www.thecssninja.com/javascript/gmail-dragout

推荐答案

关于奇怪的拖放行为,我自己已经把头发从我的头上拉了几公里.Sixlettervariables 提到的 Spy++ 可能是一个好主意.另一种方法是查看我在 SO 上的一个 Q/As 中的讨论:

I've pulled kilometers of hair out of my head myself regarding weird drag-drop behaviour. Spy++ as mentioned by sixlettervariables might be a good idea. Another would be to take a look at the discussion from one of my Q/As here at SO:

在相同的 Windows 窗体应用程序

根据我自己的经验,我似乎记得从浏览器拖放是一个安全问题,因此处理方式不同.希望这会有所帮助.

From my own experience I seem to remember that drag/drop from a browser is a security issue and thus handled differently. Hope this helps.

也许这回答了你的问题:

Maybe this answers your question:

http://www.vbforums.com/showthread.php?t=529211

这篇关于在我的 .NET Windows 窗体上实现从 Chrome 拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)