Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

c# - DragDrop text from browser to TextBox

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

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?

Thanks,

J.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...