DragDrop文本从浏览器到TextBox

我正在尝试将一些文本从网页拖放到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,但我无法弄清楚它是什么。

当然,它可能是一个浏览器,不让我拖出文本。

是什么导致了我的问题?

谢谢,

J.

这是一种通过拖放操作获取所有可用格式的数据的方法。
浏览器源工作正常(我测试了Edge,IE 11和FireFox)。

Source格式通常作为字符串或MemoryStream传递。
您可以进一步根据您的背景进行调整。

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

Google云端硬盘: 拖放示例表格

在此处输入图像描述

 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(); } public List 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 Images { get; set; } public List 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 _formats = Data.GetFormats(false).ToList(); 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(); CurrentContents.Images.AddRange( ((string[])Data.GetData(DataFormats.FileDrop, true)) .ToList() .Select(img => Bitmap.FromFile(img)) .Cast().ToArray()); break; case ("HTML Format"): CurrentContents.Format = FileDropResults.DataFormat.Html; CurrentContents.Content = Data.GetData(DataFormats.Html, true); int HtmlContentInit = CurrentContents.Content.ToString().IndexOf("", 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 DD_GetHtmlImages(string HtmlSource) { MatchCollection matches = Regex.Matches(HtmlSource, @"]+src=""([^""]*)""", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); return (matches.Count > 0) ? matches.Cast() .Select(x => x.Groups[1] .ToString()).ToList() : null; }