将文本从浏览器拖放到TextBox [英] DragDrop text from browser to TextBox

查看:85
本文介绍了将文本从浏览器拖放到TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些文本从网页拖放到Winform上的文本框.所有功能都能在MS Edge,Firefox Opera和Chrome上正常运行,但不能在IE11或Safari上运行.我在DragOver事件上使用的简短代码是:

I'm trying to drag and drop some text from a webpage to a textbox on a Winform. All works as expected from MS Edge, Firefox Opera and Chrome but not IE11 or Safari. The brief code I am using on the DragOver event is:

 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不是文本,但我不知道它是什么.

It seems like the DataFormat from IE and Safari is not Text, but I can't figure out what it is.

当然,这可能是浏览器无法阻止我拖出文字的原因.

Of course, it might be a browser thing not letting me drag text out.

有什么想法导致我的问题吗?

Any ideas what's causing my issue?

谢谢

J.

推荐答案

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

Source格式通常以字符串或MemoryStream的形式传递. 您可以根据自己的情况进一步调整它.

This is a way to get the data in all available formats from a Drag&Drop operation.
A Browser source works fine (I tested Edge, IE 11 and FireFox).

The Source formats are usually passed as strings or as a MemoryStream.
You can further adapt it to your context.

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

UPDATE:
Rebuilt the main Class object. It's now more compact and handles more details. Also, I've added a sample VS sample WinForms Form to test its results. It's a standard Form that can be included in a VS Project.

Google云端硬盘:拖动并删除;删除样本表格

Google Drive: 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;
}

这篇关于将文本从浏览器拖放到TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆