C#拖放问题 [英] C# drag-drop problem

查看:115
本文介绍了C#拖放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.NET 3.5应用程序,它允许从树中拖动项目并将它们作为文件拖放到文件夹中。这是我的代码

I have a C# .NET 3.5 application that allows drag items from a tree and drop them to a folder as a file. Here is my code

String absolutePathToFile=...get absolute path
DataObject dataObject = new DataObject();
StringCollection paths = new StringCollection();
paths.Add(absolutePathToFile);
dataObject.SetFileDropList(paths);
DoDragDrop(dataObject, DragDropEffects.Copy);

这很好用,除非与另一个通过拖放接受文件的C#应用​​程序交互时,另一个C#应用程序具有以下DragOver处理程序

This works quite well except when interacts with another C# application that accepts files via drag and drop, Another C# application has the following handler for DragOver

if ((e.Data is DataObject) && (e.Data as DataObject).ContainsFileDropList())
{
    e.Effect = DragDropEffects.Copy;
}

由于e.Data是__ComObject而不是DataObject,所以永远不会执行该块。有趣的是,当我将文件从文件夹拖到第二个应用程序上时,它将其视为DataObject。

The block is never executed since e.Data is __ComObject not DataObject. Interestingly when I drag a file from a folder over second application it sees it as DataObject.

如何使第一个C#应用程序中的DataObject在第二个C#应用程序中显示为DataObject?

How to make DataObject from first C# application appear as DataObject in second C# application?

推荐答案

我决定使用.NET Interop从IDataObjec t实现开始使用.NET Interop实现 IDataObject = http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx rel = nofollow >这里。然后我定义了 DROPFILES 结构

I decided to implement IDataObject using .NET Interop starting from IDataObject implementation from here. Then I defined DROPFILES structure

[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct _DROPFILES
{
    public Int32 pFiles;
    public Int32 X;
    public Int32 Y;
    public bool fNC;
    public bool fWide;
}

并实现填充所有OLE结构的代码。下面的函数返回我在DoDragDrop中使用的IDataObject:

and implement the code that fills all OLE structures. The function below returns IDataObject that I use in DoDragDrop:

DoDragDrop(GetDataObject(new String [] {文件名}),DragDropEffects.Copy) ;

DataObject2 GetDataObject(String[] strFiles)
{
    byte[] bData;
    _DROPFILES df = new _DROPFILES();
    int intChar, intFile, intDataLen, intPos;
    IntPtr ipGlobal = IntPtr.Zero;

    // Calculate total data length
    intDataLen = 0;
    for (intFile = 0; intFile <= strFiles.GetUpperBound(0);intFile++)
    {
        intDataLen += strFiles[intFile].Length + 1;
    }
    // Terminating double zero
    intDataLen++;

    bData = new Byte[intDataLen];
    intPos = 0;

    // Build null terminated list of files
    for (intFile = 0; intFile <= strFiles.GetUpperBound(0); intFile++)
    {
        for (intChar = 0; intChar < strFiles[intFile].Length;intChar++)
        {
            bData[intPos++] = (byte)strFiles[intFile][intChar];
        }
        bData[intPos++] = 0;
    }
    // Terminating double zero
    bData[intPos++] = 0;

    // Allocate and get pointer to global memory
    int intTotalLen = Marshal.SizeOf(df) + intDataLen;
    ipGlobal = Marshal.AllocHGlobal(intTotalLen);

    if (ipGlobal == IntPtr.Zero)
    {
        return null;
    }

    // Build DROPFILES structure in global memory.
    df.pFiles = Marshal.SizeOf(df);
    df.fWide = false;
    Marshal.StructureToPtr(df, ipGlobal, true);
    IntPtr ipNew = new IntPtr(ipGlobal.ToInt32() + Marshal.SizeOf(df));
    Marshal.Copy(bData, 0, ipNew, intDataLen);

    short CF_HDROP = 15;
    System.Runtime.InteropServices.ComTypes.FORMATETC formatEtc;
    System.Runtime.InteropServices.ComTypes.STGMEDIUM stgMedium;

    formatEtc = new System.Runtime.InteropServices.ComTypes.FORMATETC();
    formatEtc.cfFormat = CF_HDROP;
    formatEtc.dwAspect = System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT;
    formatEtc.lindex = -1;
    formatEtc.tymed = System.Runtime.InteropServices.ComTypes.TYMED.TYMED_HGLOBAL;

    stgMedium = new System.Runtime.InteropServices.ComTypes.STGMEDIUM();
    stgMedium.unionmember = ipGlobal;
    stgMedium.tymed = System.Runtime.InteropServices.ComTypes.TYMED.TYMED_HGLOBAL;
    DataObject2 dobj = new DataObject2();
    dobj.SetData(ref formatEtc, ref stgMedium, false);
    return dobj;

}

使用新代码,第二个应用程序看到 e.Data 中的DataObject ,我可以将文件拖放到任何应用程序中。除了现在,资源管理器不接受文件。我会在实现过程中错过某些东西吗?

With new code second application sees DataObject in e.Data and I can drag-drop file to any application. Except now Explorer doesn't accept files. Do I miss something in my implementation?

这篇关于C#拖放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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