2009-12-04 14 views
7

Me gustaría arrastrar y soltar un elemento de un cuadro de lista al explorador. Cuando se inicia el método de arrastrar y soltar, necesito preparar el archivo bajo demanda y guardarlo en una secuencia de memoria. ¿Puede proporcionarme un ejemplo de cómo hacerlo utilizando la estructura de datos FileGroupDescriptor? Gracias. AndreaCómo usar filegroupdescriptor para arrastrar archivos al explorador C#

Respuesta

9

Puede encontrar un ejemplo de cómo hacerlo aquí Transferring Virtual Files to Windows Explorer in C#; también hay alguna buena información sobre el tema aquí: Outlook Drag and Drop in C#

En resumen, lo que tienes que hacer es iniciar DataObject con FILEDESCRIPTOR (puedes encontrar sus detalles de declaración en pinvoke.net) estructura (s) para archivo (s) transferido y su contenido. A continuación se muestra un ejemplo de cómo transferir el archivo de winforms ListBox al explorador.

ratón hacia abajo controlador de eventos para el cuadro de lista:

private void listBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    DataObject dataObject = new DataObject(); 
    DragFileInfo filesInfo = new DragFileInfo("d:\\test.txt"); 

    using (MemoryStream infoStream = GetFileDescriptor(filesInfo), 
         contentStream = GetFileContents(filesInfo)) 
    { 
     dataObject.SetData(CFSTR_FILEDESCRIPTORW, infoStream); 
     dataObject.SetData(CFSTR_FILECONTENTS, contentStream); 
     dataObject.SetData(CFSTR_PERFORMEDDROPEFFECT, null); 

     DoDragDrop(dataObject, DragDropEffects.All); 
    } 
} 

código necesario para inicializar el DataObject:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] 
struct FILEDESCRIPTOR 
{ 
    public UInt32 dwFlags; 
    public Guid clsid; 
    public System.Drawing.Size sizel; 
    public System.Drawing.Point pointl; 
    public UInt32 dwFileAttributes; 
    public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime; 
    public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime; 
    public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime; 
    public UInt32 nFileSizeHigh; 
    public UInt32 nFileSizeLow; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] 
    public String cFileName; 
} 

public const string CFSTR_PREFERREDDROPEFFECT = "Preferred DropEffect"; 
public const string CFSTR_PERFORMEDDROPEFFECT = "Performed DropEffect"; 
public const string CFSTR_FILEDESCRIPTORW = "FileGroupDescriptorW"; 
public const string CFSTR_FILECONTENTS = "FileContents"; 

public const Int32 FD_WRITESTIME = 0x00000020; 
public const Int32 FD_FILESIZE = 0x00000040; 
public const Int32 FD_PROGRESSUI = 0x00004000; 

public struct DragFileInfo 
{ 
    public string FileName; 
    public string SourceFileName; 
    public DateTime WriteTime; 
    public Int64 FileSize; 

    public DragFileInfo(string fileName) 
    { 
     FileName = Path.GetFileName(fileName); 
     SourceFileName = fileName; 
     WriteTime = DateTime.Now; 
     FileSize = (new FileInfo(fileName)).Length; 
    } 
} 

private MemoryStream GetFileDescriptor(DragFileInfo fileInfo) 
{ 
    MemoryStream stream = new MemoryStream(); 
    stream.Write(BitConverter.GetBytes(1), 0, sizeof(UInt32)); 

    FILEDESCRIPTOR fileDescriptor = new FILEDESCRIPTOR(); 

    fileDescriptor.cFileName = fileInfo.FileName; 
    Int64 fileWriteTimeUtc = fileInfo.WriteTime.ToFileTimeUtc(); 
    fileDescriptor.ftLastWriteTime.dwHighDateTime = (Int32)(fileWriteTimeUtc >> 32); 
    fileDescriptor.ftLastWriteTime.dwLowDateTime = (Int32)(fileWriteTimeUtc & 0xFFFFFFFF); 
    fileDescriptor.nFileSizeHigh = (UInt32)(fileInfo.FileSize >> 32); 
    fileDescriptor.nFileSizeLow = (UInt32)(fileInfo.FileSize & 0xFFFFFFFF); 
    fileDescriptor.dwFlags = FD_WRITESTIME | FD_FILESIZE | FD_PROGRESSUI; 

    Int32 fileDescriptorSize = Marshal.SizeOf(fileDescriptor); 
    IntPtr fileDescriptorPointer = Marshal.AllocHGlobal(fileDescriptorSize); 
    Byte[] fileDescriptorByteArray = new Byte[fileDescriptorSize]; 

    try 
    { 
     Marshal.StructureToPtr(fileDescriptor, fileDescriptorPointer, true); 
     Marshal.Copy(fileDescriptorPointer, fileDescriptorByteArray, 0, fileDescriptorSize); 
    } 
    finally 
    { 
     Marshal.FreeHGlobal(fileDescriptorPointer); 
    } 
    stream.Write(fileDescriptorByteArray, 0, fileDescriptorByteArray.Length); 
    return stream; 
} 

private MemoryStream GetFileContents(DragFileInfo fileInfo) 
{ 
    MemoryStream stream = new MemoryStream();    
    using (BinaryReader reader = new BinaryReader(File.OpenRead(fileInfo.SourceFileName))) 
    { 
     Byte[] buffer = new Byte[fileInfo.FileSize]; 
     reader.Read(buffer, 0, (Int32)fileInfo.FileSize); 
     if (buffer.Length == 0) buffer = new Byte[1]; 
     stream.Write(buffer, 0, buffer.Length); 
    } 
    return stream; 
} 

esperanza esto le dará una idea sobre cómo proceder, respecto

+0

Soy muy pobre en C++, pero ¿tiene esto el mismo problema de tener el operador '' sizeof() 'incorrecto usado como se discute en este artículo? http://www.codeproject.com/KB/cs/UnmanagedArraysInCSharp.aspx Dicen que 'Marshal.SizeOf' da el tamaño DESPUÉS de ordenar. – Maslow

Cuestiones relacionadas