2009-12-04 58 views

回答

9

你可以在這裏找到一個關於如何做到這一點的例子Transferring Virtual Files to Windows Explorer in C#;關於這個主題的一些很好的信息在這裏:Outlook Drag and Drop in C#

總之你要做的就是用FILEDESCRIPTOR初始化DataObject(你可以在pinvoke.net上找到它的聲明細節)文件獲取的結構(s)轉移和他們的內容。下面是如何將文件從winforms ListBox轉移到資源管理器的示例。

鼠標按下事件處理程序列表框:給init數據對象需要

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); 
    } 
} 

代碼:

[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; 
} 

希望這會給你如何進行的一個想法, 問候

+0

我在C++上真的很差,但是這樣做是否與本文中討論的使用了錯誤的'sizeof()'運算符相同? http://www.codeproject.com/KB/cs/UnmanagedArraysInCSharp.aspx 他們說'Marshal.SizeOf'給出了編組後的大小。 – Maslow 2010-06-22 13:03:27

相關問題