Exemplo n.º 1
0
        /// <summary>
        /// Static constructor to create an instance from the current clipboard data
        /// </summary>
        /// <returns></returns>
        public static ClipboardContents FromClipboard()
        {
            var container = new ClipboardContents {
                Timestamp = DateTime.Now
            };

            // Read all supported clipboard data
            // https://docs.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats

            if (Clipboard.ContainsImage())
            {
                container.Contents.Add(new ImageContent(Clipboard.GetImage()));
            }
            if (Clipboard.ContainsData(DataFormats.Html))
            {
                container.Contents.Add(new HtmlContent(ReadClipboardHtml()));
            }
            if (Clipboard.ContainsData(DataFormats.CommaSeparatedValue))
            {
                container.Contents.Add(new CsvContent(ReadClipboardString(DataFormats.CommaSeparatedValue)));
            }
            if (Clipboard.ContainsData(DataFormats.SymbolicLink))
            {
                container.Contents.Add(new SylkContent(ReadClipboardString(DataFormats.SymbolicLink)));
            }
            if (Clipboard.ContainsData(DataFormats.Rtf))
            {
                container.Contents.Add(new RtfContent(ReadClipboardString(DataFormats.Rtf)));
            }
            if (Clipboard.ContainsData(DataFormats.Dif))
            {
                container.Contents.Add(new DifContent(ReadClipboardString(DataFormats.Dif)));
            }

            if (Clipboard.ContainsFileDropList() && !Clipboard.ContainsText())
            {
                // save list of file paths instead
                container.Contents.Add(new TextContent(string.Join("\n", Clipboard.GetFileDropList().Cast <string>().ToList())));
            }

            if (Clipboard.ContainsText() && Uri.IsWellFormedUriString(Clipboard.GetText().Trim(), UriKind.RelativeOrAbsolute))
            {
                container.Contents.Add(new UrlContent(Clipboard.GetText().Trim()));
            }

            // make sure text content comes last, as it may includes extensions from previous formats
            if (Clipboard.ContainsText())
            {
                container.Contents.Add(new TextContent(Clipboard.GetText()));
            }


            return(container);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read the clipboard and update the UI
        /// </summary>
        /// <returns></returns>
        private bool readClipboard()
        {
            clipData = ClipboardContents.FromClipboard();

            // Update extension dropdown list
            comExt.Items.Clear();
            foreach (var content in clipData.Contents)
            {
                comExt.AddWithSeparator(content.Extensions.Except(comExt.ItemArray()));
            }

            // if selected extension does not match available contents, adjust it
            if (comExt.Text == "*" || comExt.Text == null || clipData.ForExtension(comExt.Text) == null)
            {
                // chose file extension based on available contents in this order
                BaseContent content = clipData.PrimaryContent;
                if (content is ImageContent)
                {
                    comExt.Text = content.Extensions.Contains(Settings.Default.extensionImage) ? Settings.Default.extensionImage : content.DefaultExtension;
                }
                else if (content is TextContent)
                {
                    comExt.Text = Settings.Default.extensionText == null ? content.DefaultExtension : Settings.Default.extensionText;
                }
                else if (content != null)
                {
                    comExt.Text = content.DefaultExtension;
                }
                else
                {
                    comExt.Text = "";
                }
            }

            if (comExt.Items.Count > 0)
            {
                updateContentPreview();
                return(true);
            }

            MessageBox.Show(Resources.str_noclip_text, Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return(false);
        }