2013-07-03 3 views
9

ユースケース:ユーザーはOutlookからドロップする電子メールアイテムをWinForms(.Net 4)アプリケーションのフォームにドラッグする機能が必要です。アプリケーションはこれらの項目を.msg形式で保存し、それらを所定の場所に保存します。C#WinForms:ドラッグドロップアクションイベントのタイプを確認

問題:私のコードは、他のソースからのドラッグ・ドロップに対して堅牢ではありません(IEからフォームへjpegをドラッグすると同じイベントがトリガーされます)。これは、ドラッグされたアイテムが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" 
+1

はまだ私の質問に明確な答えを見つけることができますが、この記事では、最も近い来ました:http://www.codeproject.com/Articles/28209/Outlook-Drag-and- Drop-in-C –

答えて

0

私はここにすることができソリューションのソースコードを持っています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(); 
     } 
    } 
+0

FileGroupDescriptorまたはFileGroupDescriptorWの存在により、Outlookがソースであったことが示唆される理由はありますか?同じプロパティを持つ可能性のある他のアプリケーションはありますか? –

+0

私は知らないけど、それはあなたがGoogleについて語るべきものです。私の例はあなたのために働いたのですか? –

+0

あなたの例のように、外部コードが必要です... OutlookDataObject? – menssana

関連する問題