e.Data.GetDataPresent在WinForms拖放处理程序中不起作用? [英] e.Data.GetDataPresent not working in WinForms drag-and-drop handler?

查看:959
本文介绍了e.Data.GetDataPresent在WinForms拖放处理程序中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从名为Locate32的程序(这是很好的方式)将文件拖到我的应用程序中。这是发生了什么:

I'm trying to drag files into my application from a program called Locate32 (which is great by the way). Here is what happens:

e.Data.GetFormats()
{string[7]}
    [0]: "FileDrop"
    [1]: "FileNameW"
    [2]: "FileName"
    [3]: "FileNameMap"
    [4]: "FileNameMapW"
    [5]: "Shell IDList Array"
    [6]: "Shell Object Offsets"
DataFormats.FileDrop
"FileDrop"
e.Data.GetDataPresent(DataFormats.FileDrop)
false

为什么 e.Data .GetDataPresent(DataFormats.FileDrop) return false 即使FileDrop显然是列为可用的格式之一?

Why does e.Data.GetDataPresent(DataFormats.FileDrop) return false even though FileDrop is clearly one of the formats listed as "available"?

如果我做 e.Data.GetData(DataFormats.FileDrop)我得到一堆文件名列表,我应该。此外,拖放可以从Windows资源管理器中正常工作。

If I do e.Data.GetData(DataFormats.FileDrop) I get a list of a bunch of filenames, as I should. Also, drag and drop works fine from Windows Explorer.

这是DragEnter处理程序的代码:

Here's the code for my DragEnter handler:

private void MyForm_DragEnter(object sender, DragEventArgs e) {
    if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
        e.Effect = DragDropEffects.Copy;
    } else {
        e.Effect = DragDropEffects.None;
    }
}


推荐答案

你应该查看 e.AllowedEffect 如果 DragDropEffects.Copy 在列表中。

You should take a look into e.AllowedEffect if DragDropEffects.Copy is within the list.

前一段时间,我也有一些问题,从 GetDataPresent()中获取正确的格式。由于这个事实,我只是直接查看由 GetFormats()提供的列表,并自己做。代码是这样的:

Some time ago i also had some problems with getting the right format out of the GetDataPresent(). Due to this fact, i just looked directly into the list provided by GetFormats() and did it on myself. The code was something like that:

private void OnItemDragEnter(object sender, DragEventArgs e)
{
    //Get the first format out of the list and try to cast it into the
    //desired type.
    var list = e.Data.GetData(e.Data.GetFormats()[0]) as IEnumerable<ListViewItem>;
    if (list != null)
    {
        e.Effect = DragDropEffects.Copy;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

这个简单的解决方案适用于我,但你也可以遍历 GetFormats()数组与linq,并尝试通过 IEnumerable< T> .OfType< MyType>() 或类似的东西。

This simple solution works for me, but you could also walk all over the GetFormats() array with linq and try to find your desired type by IEnumerable<T>.OfType<MyType>() or something similar.

这篇关于e.Data.GetDataPresent在WinForms拖放处理程序中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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