C#WinForms:识别拖放操作事件的类型

Vis*_*loi 9 c# drag-and-drop winforms

用例:用户需要能够将Outlook中的电子邮件项目拖放到WinForms(.Net 4)应用程序中的表单上.应用程序以.msg格式保存这些项目并将它们存储在预定位置.

问题是:我的代码不能很好地防止来自其他来源的拖放(例如,将jpeg从IE拖到窗体上会触发相同的事件).这是因为我无法确定拖动的项目是Outlook对象,还是拖动项目的来源.

有没有解决方法,我只能接受特定类型的拖放项目?这是我在DragDrop事件处理程序中的代码:

Outlook.Application outlook = new Outlook.Application();
Outlook.Selection sel = outlook.ActiveExplorer().Selection;

try
{    
    foreach (object item in sel)
    {
        if (item is Outlook.MailItem)
        {
            var mail = item as Outlook.MailItem;

            CopyMailItemToLocalTempDir(mail);

            txtComment.Text = "Email from " + mail.SenderName + " Regarding " + mail.Subject;
        }
    }
}
finally
{
    // This is hokey but it prevents Outlook from remembering previously selected items
    // - refer http://stackoverflow.com/questions/14090420/interop-outlook-doesnt-clear-selected-mails-at-drag-and-drop
    var folder = outlook.ActiveExplorer().CurrentFolder;
    outlook.ActiveExplorer().CurrentFolder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    Thread.Sleep(50);
    outlook.ActiveExplorer().CurrentFolder = folder;

    Marshal.ReleaseComObject(sel);
    Marshal.ReleaseComObject(outlook);
    sel = null;
    outlook = null;
}
Run Code Online (Sandbox Code Playgroud)

从Outlook拖动时DragEventArgs对象(e)的一些详细信息:

e.Data.GetFormats() returns: 
{string[15]}
    [0]: "RenPrivateSourceFolder"
    [1]: "RenPrivateLatestMessages"
    [2]: "RenPrivateMessages"
    [3]: "RenPrivateItem"
    [4]: "FileGroupDescriptor"
    [5]: "FileGroupDescriptorW"
    [6]: "FileDrop"
    [7]: "FileNameW"
    [8]: "FileName"
    [9]: "FileContents"
    [10]: "Object Descriptor"
    [11]: "System.String"
    [12]: "UnicodeText"
    [13]: "Text"
    [14]: "CSV"

e.Data.GetData("Text") returns: 
"From\tSubject\tReceived\tSize\tCategories\t\r\nJoe Sender\tThis is the email subject\t10:51 AM\t3 KB\t\t"
Run Code Online (Sandbox Code Playgroud)

No *_*ame 1

我这里有解决方案的源代码,仅允许删除 Outlook 项目。这是事件处理程序:

private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        //display formats available
        this.label1.Text = "Formats:\n";
        foreach (string format in e.Data.GetFormats())
        {
            this.label1.Text += "    " + format + "\n";
        }

        //ensure FileGroupDescriptor is present before allowing drop
        if (e.Data.GetDataPresent("FileGroupDescriptor"))
        {
            e.Effect = DragDropEffects.All;
        }
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        //wrap standard IDataObject in OutlookDataObject
        OutlookDataObject dataObject = new OutlookDataObject(e.Data);

        //get the names and data streams of the files dropped
        string[] filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
        MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents");

        this.label1.Text += "Files:\n";
        for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
        {
            //use the fileindex to get the name and data stream
            string filename = filenames[fileIndex];
            MemoryStream filestream = filestreams[fileIndex];
            this.label1.Text += "    " + filename + "\n";

            //save the file stream using its name to the application path
            FileStream outputStream = File.Create(filename);
            filestream.WriteTo(outputStream);
            outputStream.Close();
        }
    }
Run Code Online (Sandbox Code Playgroud)