从 Outlook 拖放到 Winforms
Drag and Drop from Outlook into Winforms
当将项目从 Outlook 电子邮件拖到 Winforms 应用程序时(控件是 DevExpress 的 GalleryControl
,DragDrop 事件没有触发,即使我在 DragEnter 事件中手动设置了“DragDropEffects.Move”处理程序。(已确认正在触发)
但是,仅当从 windows 资源管理器中拖动普通文件时,DragDrop 事件才会触发。
private async void gcImages_DragDrop(object sender, DragEventArgs e)
{
string[] fileNames = null;
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
OutlookDataObject dataObject = new OutlookDataObject(e.Data);
string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor");
}
// do stuff async with file names
}
private void gcImages_DragEnter(object sender, DragEventArgs e)
{
// This event fires, no matter what i drag onto it. (Files from explorer, attachments from Outlook etc)
// However even after setting the effect as per below, the cursor still shows the 'not allowed' symbol.
e.Effect = DragDropEffects.Move;
}
我在控件上启用了 AllowDrop = true
,它可以与 Windows Explorer 文件完美配合,但不适用于 outlook 文件。
奇怪的是 DragEnter 事件正在触发,但 DragDrop 事件不会触发 Outlook 附件。
最终使用了这段代码,似乎工作正常。
//// Use Like This
private void gcImages_DragDrop(object sender, DragEventArgs e)
{
DragDropHelper.AcceptDroppedFile(e, AddAndSaveNewDocument);
}
private void AddAndSaveNewDocument(FileSystemInfo fileInfo)
{
}
////
public static class DragDropHelper
{
public static void AcceptDroppedFile(DragEventArgs e, Action<FileInfo> addAndSaveNewDocument)
{
string[] fileNames = null;
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
var dataObject = new OutlookDataObject(e.Data);
fileNames = (string[])dataObject.GetData("FileGroupDescriptor");
for (var i = 0; i < fileNames.Length; i++)
{
var itm = fileNames[i];
using var ms = dataObject.GetData("FileContents", i);
var tmpFileName = Path.Combine(Path.GetTempPath(), itm);
using (var file = new FileStream(tmpFileName, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
file.Write(bytes, 0, bytes.Length);
ms.Close();
}
fileNames[i] = tmpFileName;
}
}
if (fileNames != null)
{
foreach (var fileName in fileNames)
{
var fileInfo = new FileInfo(fileName);
addAndSaveNewDocument(fileInfo);
if (fileName.Contains(Path.GetTempPath(), StringComparison.CurrentCultureIgnoreCase))
{
File.Delete(fileName);
}
}
}
}
}
此处的代码用于 OutlookDataObject
class https://codeshare.io/G7747D
当将项目从 Outlook 电子邮件拖到 Winforms 应用程序时(控件是 DevExpress 的 GalleryControl
,DragDrop 事件没有触发,即使我在 DragEnter 事件中手动设置了“DragDropEffects.Move”处理程序。(已确认正在触发)
但是,仅当从 windows 资源管理器中拖动普通文件时,DragDrop 事件才会触发。
private async void gcImages_DragDrop(object sender, DragEventArgs e)
{
string[] fileNames = null;
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
OutlookDataObject dataObject = new OutlookDataObject(e.Data);
string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor");
}
// do stuff async with file names
}
private void gcImages_DragEnter(object sender, DragEventArgs e)
{
// This event fires, no matter what i drag onto it. (Files from explorer, attachments from Outlook etc)
// However even after setting the effect as per below, the cursor still shows the 'not allowed' symbol.
e.Effect = DragDropEffects.Move;
}
我在控件上启用了 AllowDrop = true
,它可以与 Windows Explorer 文件完美配合,但不适用于 outlook 文件。
奇怪的是 DragEnter 事件正在触发,但 DragDrop 事件不会触发 Outlook 附件。
最终使用了这段代码,似乎工作正常。
//// Use Like This
private void gcImages_DragDrop(object sender, DragEventArgs e)
{
DragDropHelper.AcceptDroppedFile(e, AddAndSaveNewDocument);
}
private void AddAndSaveNewDocument(FileSystemInfo fileInfo)
{
}
////
public static class DragDropHelper
{
public static void AcceptDroppedFile(DragEventArgs e, Action<FileInfo> addAndSaveNewDocument)
{
string[] fileNames = null;
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{
var dataObject = new OutlookDataObject(e.Data);
fileNames = (string[])dataObject.GetData("FileGroupDescriptor");
for (var i = 0; i < fileNames.Length; i++)
{
var itm = fileNames[i];
using var ms = dataObject.GetData("FileContents", i);
var tmpFileName = Path.Combine(Path.GetTempPath(), itm);
using (var file = new FileStream(tmpFileName, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
file.Write(bytes, 0, bytes.Length);
ms.Close();
}
fileNames[i] = tmpFileName;
}
}
if (fileNames != null)
{
foreach (var fileName in fileNames)
{
var fileInfo = new FileInfo(fileName);
addAndSaveNewDocument(fileInfo);
if (fileName.Contains(Path.GetTempPath(), StringComparison.CurrentCultureIgnoreCase))
{
File.Delete(fileName);
}
}
}
}
}
此处的代码用于 OutlookDataObject
class https://codeshare.io/G7747D