【问题标题】:DragDrop text from browser to TextBox将文本从浏览器拖放到文本框
【发布时间】:2018-08-05 07:18:39
【问题描述】:

我正在尝试将一些文本从网页拖放到 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.

【问题讨论】:

  • 您好,IE 对将数据复制到剪贴板和在不同窗口之间拖动内容有安全限制。默认情况下,Internet 区域中的域提示访问剪贴板/内存并禁用拖放。其他浏览器(如 safari)具有逐个站点的安全粒度,可能会或可能不会通过全局策略进行集中管理。你可以将你的 html 和 javascript 发布到 jsfiddle 吗?

标签: c# winforms internet-explorer safari


【解决方案1】:

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

源格式通常以字符串或MemoryStream 的形式传递。
您可以进一步调整它以适应您的环境。

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

Google 云端硬盘:Drag & Drop sample Form



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

【讨论】:

  • 这是一个非常全面的答案,我还没有尝试。但是,如果浏览器没有释放文本,那么就没有什么可以丢弃的了。一旦我试一试,我就会更新。谢谢。
  • @Jason James 我做了一个(有点重)编辑。现在,当 Drop 包含多个源(html 文本和图像)时,处理 Html 图像会更容易。直接图像滴立即可用(所有最终都在 List 属性 + 一些信息中)。 HTTP 源已列出但未下载..
  • @Jason James 我看到你已经接受了答案,但是,考虑到已经过去的时间,我认为你这样做是出于礼貌。我查看了代码,我不得不承认,这并不容易理解。于是,我重新写了一遍。主类应该更容易处理,但它仍然很复杂。所以我准备了一个可以包含在任何 Winforms 项目中的示例表单。它是自给自足的。它应该让您更清楚地了解它可以做什么。您只需选择 Html 页面的任何部分并将其拖放到支持 Drop 的两个控件之一上。也许,让我知道你的想法。
猜你喜欢
  • 1970-01-01
  • 2019-10-10
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2011-07-21
  • 2012-02-04
相关资源
最近更新 更多