C# drag-drop problem
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, Dra开发者_开发技巧gDropEffects.Copy);
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;
}
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.
How to make DataObject from first C# application appear as DataObject in second C# application?
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;
}
and implement the code that fills all OLE structures. The function below returns IDataObject that I use in DoDragDrop:
DoDragDrop(GetDataObject(new String[] { file name }), 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;
}
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?
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论