11

ユース ケース:ユーザーは、電子メール アイテムを 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;
}

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"
4

2 に答える 2

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();
        }
    }
于 2013-07-04T10:05:35.693 に答える