簡體   English   中英

從 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 資源管理器文件完美兼容,但不適用於 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類的代碼https://codeshare.io/G7747D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM