Beispiel #1
0
        /// <summary>
        /// Copies the data of the given item to the clipboard
        /// </summary>
        public bool CopyDataToClipboard(IClipboardItem itemToCopy)
        {
            if (!itemToCopy.HasData)
            {
                m_status = $" Failed to copy data to the clipboard. Given item has no data";
                return(false);
            }

            bool clipboardBlocked = false;

            switch (itemToCopy.Type)
            {
            case DataKind.PlainText:
                try
                {
                    Clipboard.SetText(((PlainTextItem)itemToCopy).GetText());
                }
                catch (Exception)
                {
                    try
                    {
                        FreeAndClearClipboard();
                        Clipboard.SetText(((PlainTextItem)itemToCopy).GetText());
                    }
                    catch (Exception)
                    {
                        clipboardBlocked = true;
                    }
                }
                break;

            case DataKind.Image:
                try
                {
                    Clipboard.SetImage(((ImageItem)itemToCopy).GetImage());
                }
                catch (Exception)
                {
                    try
                    {
                        FreeAndClearClipboard();
                        Clipboard.SetImage(((ImageItem)itemToCopy).GetImage());
                    }
                    catch (Exception)
                    {
                        clipboardBlocked = true;
                    }
                }
                break;

            default:
                m_status = $" Failed to copy data to the clipboard. Unsupported item type: {itemToCopy.Type.ToString()}";
                return(false);
            }

            if (clipboardBlocked)
            {
                string blockingWindow = StaticHelper.GetOpenClipboardWindowInfo();
                m_status = $" Failed to copy data to the clipboard. Cliboard is blocked by another process: {blockingWindow}";
                return(false);
            }

            return(true);
        }