在我的.NET Windows窗体上从Chrome实现拖放 [英] Implementing drag-drop from Chrome on my .NET Windows form

查看:139
本文介绍了在我的.NET Windows窗体上从Chrome实现拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Chrome具有方便的功能,可以点击下载链接并将其拖动到Windows资源管理器窗口中,然后删除。放弃后,Chrome会下载该文件,并将其放在我放下的位置。



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

  Private Sub gridFiles_DragDrop(ByVal sender As Object,ByVal e As System。 Windows.Forms.DragEventArgs)处理gridFiles.DragDrop 
如果e.Data.GetDataPresent(DataFormats.FileDrop)然后
Dim DroppedFiles()As String = e.Data.GetData(DataFormats.FileDrop)
如果没有DroppedFiles是没有
对于每个文件As String在DroppedFiles
MsgBox(文件)
下一个
如果
结束If
End Sub

Private Sub gridFiles_DragEnter(ByVal sender As Object,ByVal e As System.Windows.Forms.DragEventArgs)处理gridFiles.DragEnter
如果e.Data.GetDataPresent(DataFormats.FileDrop)然后
e.Effect = DragDropEffects.All
如果

End Sub

当我从Windows资源管理器中删除文件时,一切正常,我会收到每个文件的消息框下降。但是,当我从Chrome中删除时,没有任何反应。原因是 DroppedFiles 等于 Nothing 。似乎 e.Data.GetData 不返回任何东西。我已经使用 e.Data.GetFormats()检查格式,并返回 FileDrop 文件名 FileNameW 如预期的任何文件丢失。



我相当确定正在发生的是,Chrome表示它有一些文件,以便 DragEnter 功能,但由于尚未下载该文件, DragDrop 无法完成,所以Chrome不会返回文件。我怀疑在Windows资源管理器的上下文中,Chrome以某种方式获取该窗口的路径并稍后复制文件。



所以我的问题是...



我如何愚弄Google Chrome进入我的应用程序?我看到这个工作通过某种方式给Chrome一个临时文件夹,认为它已经放弃了文件,我的应用程序将监视该文件夹的新文件,并在下载后将其拉入。我只需要找到一种Chrome浏览器知道该文件夹的方法。



或者,如果我可以得到被删除的URL,那就好了好。我可以用我的程序下载文件。



任何和所有的建议是非常感激的。谢谢。






编辑:所以看起来,使用常规的URL,拖入UniformResourceLocator格式。我看到的行为与Gmail中的下载链接有关。它可能发生在别的地方,但我不确定。当gmail附件被从Gmail拖到我的应用程序中时,我得到一个FileDrop。



做更多的挖掘,似乎Gmail正在使用锚标签的download_url 属性。我从来没有听说过这个。也许这只是他们添加的额外财产?



在任何情况下,由于我的应用程序将主要用于电子邮件附件,我需要一个幻影FileDrop可以正常工作,如上所述。我无法使用Spy ++。当出现丢失时似乎没有显示任何消息。 (我也欢迎有关该问题的任何建议。)



编辑#2:有关Gmail如何使用拖放功能的更多信息档案: http://www.thecssninja.com/javascript/gmail-dragout

解决方案

我已经把我头顶上的公里的头发放在了我自己的怪异的拖放行为上。六角变量提到的间谍++可能是个好主意。另一个是看看我在这里的一个问题的讨论:



在同一Windows窗体应用程序的实例之间拖放



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



编辑:



也许这回答你的问题: / p>

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


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.

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

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.

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.

So my question is...

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.

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.


EDIT: 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.

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?

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.)

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

解决方案

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:

Drag and Drop between Instances of the same Windows Forms Application

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.

EDIT:

Maybe this answers your question:

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

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

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