【问题标题】:Drag and Drop from Outlook into Winforms从 Outlook 拖放到 Winforms
【发布时间】:2020-02-18 05:36:05
【问题描述】:

当将项目从 Outlook 电子邮件拖到 Winforms 应用程序时(控件是 DevExpress 的 GalleryControl,即使我在 DragEnter 事件处理程序中手动设置了“DragDropEffects.Move”,DragDrop 事件也不会触发。(已确认这是开火)

但是 DragDrop 事件只会在从 Windows 资源管理器中拖动普通文件时触发。

    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 资源管理器文件配合使用,但不能与 Outlook 文件配合使用。

奇怪的是 DragEnter 事件正在触发,但 DragDrop 事件不会与 Outlook 附件一起触发。

【问题讨论】:

  • 您可能会遇到低权限进程无法拖放到高权限应用程序的限制。
  • 无法复制,使用不同类型的用户 (Outlook 2016/x64)。该对象包含FILEDESCRIPTORW 结构的数组。 MemoryStream 的第一个字节包含结构的数量。这些结构返回附件的文件名,而FileContents 格式返回包含附件字节的 MemoryStream。在标准 WinForms 控件上拖放。尝试使用具有不同用户权限的非 DevExpress 控件来测试在您的上下文中有效的方法。
  • 设法让它与 Outlook 2019 一起使用,使用的是 2013 吗?知道为什么会这样吗?

标签: c# winforms outlook drag-and-drop idataobject


【解决方案1】:

最终使用此代码,似乎工作正常。

//// 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);
                }
            }
        }
    }
}

此处为OutlookDataObjecthttps://codeshare.io/G7747D 的代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 2012-10-10
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多