DragDrop text from browser to TextBox

后端 未结 1 420
一生所求
一生所求 2020-12-12 07:19

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 b

相关标签:
1条回答
  • 2020-12-12 08:05

    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



    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;
    }
    
    0 讨论(0)
提交回复
热议问题