用例:用户需要具备拖放功能将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"
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
是否有一种解决方法,使得我只能接受 特定类型?
(使用单个文件时):
if (e.Data.GetData(typeof(...)) is ...)
//process it
和
if (e.Data.GetDataPresent(typeof(...)))
//process it
由于您没有将 类型 指定为Outlook.MailItem
(或者也许是Image
),但没有将来源指定为:我认为您的程序在代码的其他部分失败(由于设计不良)。例如。第二行或ActiveExplorer().Selection
部分中的finally {}
。通过就地进行预过滤/尝试捕获(在涉及Outlook之前)或在DragEnter
事件中(最好在类型严格为某种Outlook类型时更好),可以轻松地进行管理。
要掌握拖放内容,请check out this。
或者对于Outlook消息之类的特殊类型,您已经有了一个不错的解决方案,article you mentioned中有很多解释。
我还发现an answer to your non-general (outlook only) use case(2012年撰写)展示了 OutlookDataObject 类,并且可能基于您提到的文章(2008年撰写)。
>