DragDrop文本从浏览器到TextBox

时间:2018-02-25 20:43:35

标签: c# winforms internet-explorer safari

我试图将一些文本从网页拖放到Winform上的文本框中。所有这些都可以从MS Edge,Firefox Opera和Chrome中获得,但不是IE11或Safari。我在DragOver事件中使用的简短代码是:

 private void textBox1_DragDrop(object sender, DragEventArgs e)
 {
        if(e.Data.GetDataPresent(DataFormats.Text, false))
        {
            textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
        }
 }

似乎IE和Safari的DataFormat不是Text,但我无法弄清楚它是什么。

当然,浏览器可能不会让我拖出文本。

任何想法导致我的问题?

谢谢,

学家

1 个答案:

答案 0 :(得分:0)

这是一种通过拖放操作获取所有可用格式数据的方法 浏览器源工作正常(我测试了Edge,IE 11和FireFox)
源格式通常作为字符串或MemoryStream传递 您可以根据自己的情况进一步调整。

<强>更新
重建主类对象。它现在更紧凑,处理更多细节。另外,我添加了一个样本VS样本WinForms表单来测试其结果。这是一个可以包含在VS项目中的标准表格。

Google云端硬盘:Drag & Drop sample Form

Drag & Drop results C# project

using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;


private FileDropResults DD_Results;

public class FileDropResults
{
    public enum DataFormat : int
    {
        MemoryStream = 0,
        Text,
        UnicodeText,
        Html,
        Bitmap,
        ImageBits,
    }

    public FileDropResults() { this.Contents = new List<DropContent>(); }

    public List<DropContent> Contents { get; set; }

    public class DropContent
    {
        public object Content { get; set; }
        public string Result { get; set; }
        public DataFormat Format { get; set; }
        public string DataFormatName { get; set; }
        public List<Bitmap> Images { get; set; }
        public List<string> HttpSourceImages { get; set; }
    }
}

private void TextBox1_DragDrop(object sender, DragEventArgs e)
{
    GetDataFormats(e.Data);
}

private void TextBox1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

private void GetDataFormats(IDataObject Data)
{
    DD_Results = new FileDropResults();
    List<string> _formats = Data.GetFormats(false).ToList<string>();

    foreach (string _format in _formats)
    {
        FileDropResults.DropContent CurrentContents = new FileDropResults.DropContent() 
        { DataFormatName = _format };

        switch (_format)
        {
            case ("FileGroupDescriptor"):
            case ("FileGroupDescriptorW"):
            case ("DragContext"):
            case ("UntrustedDragDrop"):
                break;
            case ("DragImageBits"):
                CurrentContents.Content = (MemoryStream)Data.GetData(_format, true);
                CurrentContents.Format = FileDropResults.DataFormat.ImageBits;
                break;
            case ("FileDrop"):
                CurrentContents.Content = null;
                CurrentContents.Format = FileDropResults.DataFormat.Bitmap;
                CurrentContents.Images = new List<Bitmap>();
                CurrentContents.Images.AddRange(
                    ((string[])Data.GetData(DataFormats.FileDrop, true))
                    .ToList()
                    .Select(img => Bitmap.FromFile(img))
                    .Cast<Bitmap>().ToArray());
                break;
            case ("HTML Format"):
                CurrentContents.Format = FileDropResults.DataFormat.Html;
                CurrentContents.Content = Data.GetData(DataFormats.Html, true);
                int HtmlContentInit = CurrentContents.Content.ToString().IndexOf("<html>", StringComparison.InvariantCultureIgnoreCase);
                if (HtmlContentInit > 0)
                    CurrentContents.Content = CurrentContents.Content.ToString().Substring(HtmlContentInit);
                CurrentContents.HttpSourceImages = DD_GetHtmlImages(CurrentContents.Content.ToString());
                break;
            case ("UnicodeText"):
                CurrentContents.Format = FileDropResults.DataFormat.UnicodeText;
                CurrentContents.Content = Data.GetData(DataFormats.UnicodeText, true);
                break;
            case ("Text"):
                CurrentContents.Format = FileDropResults.DataFormat.Text;
                CurrentContents.Content = Data.GetData(DataFormats.Text, true);
                break;
            default:
                CurrentContents.Format = FileDropResults.DataFormat.MemoryStream;
                CurrentContents.Content = Data.GetData(_format, true);
                break;
        }

        if (CurrentContents.Content != null)
        {
            if (CurrentContents.Content.GetType() == typeof(MemoryStream))
            {
                using (MemoryStream _memStream = new MemoryStream())
                {
                    ((MemoryStream)CurrentContents.Content).CopyTo(_memStream);
                    _memStream.Position = 0;

                    CurrentContents.Result = Encoding.Unicode.GetString(_memStream.ToArray());
                }
            }
            else
            {
                if (CurrentContents.Content.GetType() == typeof(String))
                    CurrentContents.Result = CurrentContents.Content.ToString();
            }
        }
        DD_Results.Contents.Add(CurrentContents);
    }
}

public List<string> DD_GetHtmlImages(string HtmlSource)
{
    MatchCollection matches = Regex.Matches(HtmlSource, @"<img[^>]+src=""([^""]*)""",
                              RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
    return (matches.Count > 0)
            ? matches.Cast<Match>()
                    .Select(x => x.Groups[1]
                    .ToString()).ToList()
            : null;
}