Ejemplo n.º 1
0
        /// <summary>
        /// Adds text.
        /// Returns this.
        /// </summary>
        /// <param name="text">Text.</param>
        /// <param name="format">
        /// Clipboard format id. Default: <see cref="ClipFormats.Text"/> (CF_UNICODETEXT).
        /// Text encoding (UTF-16, ANSI, etc) depends on format; default UTF-16. See <see cref="ClipFormats.Register"/>.
        /// </param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException">Invalid format.</exception>
        /// <exception cref="Exception">Exceptions of <see cref="Encoding.GetBytes(string)"/>, which is called if encoding is not UTF-16.</exception>
        public AClipboardData AddText(string text, int format = ClipFormats.Text)
        {
            Encoding enc = ClipFormats.GetTextEncoding_(format, out bool unknown);

            if (enc == null)
            {
                return(_Add(text, format == 0 ? Api.CF_UNICODETEXT : format));
            }
            return(_Add(enc.GetBytes(text + "\0"), format));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets clipboard text without open/close.
        /// If format is 0, tries CF_UNICODETEXT and CF_HDROP.
        /// </summary>
        internal static unsafe string GetText_(int format)
        {
            IntPtr h = default;

            if (format == 0)
            {
                h = Api.GetClipboardData(Api.CF_UNICODETEXT);
                if (h == default)
                {
                    format = Api.CF_HDROP;
                }
            }
            if (format == 0)
            {
                format = Api.CF_UNICODETEXT;
            }
            else
            {
                h = Api.GetClipboardData(format); if (h == default)
                {
                    return(null);
                }
                if (format == Api.CF_HDROP)
                {
                    return(string.Join("\r\n", _HdropToFiles(h)));
                }
            }

            using (new _GlobalLock(h, out var mem, out int len)) {
                if (mem == default)
                {
                    return(null);
                }
                var s = (char *)mem; var b = (byte *)s;

                Encoding enc = ClipFormats.GetTextEncoding_(format, out bool unknown);
                if (unknown)
                {
                    if ((len & 1) != 0 || Util.BytePtr_.Length(b, len) > len - 2)
                    {
                        enc = Encoding.Default;                                                                              //autodetect
                    }
                }

                if (enc == null)
                {
                    len /= 2; while (len > 0 && s[len - 1] == '\0')
                    {
                        len--;
                    }
                    return(new string(s, 0, len));
                }
                else
                {
                    //most apps add single '\0' at the end. Some don't add. Some add many, eg Dreamweaver. Trim all.
                    int charLen = enc.GetByteCount("\0");
                    switch (charLen)
                    {
                    case 1:
                        while (len > 0 && b[len - 1] == '\0')
                        {
                            len--;
                        }
                        break;

                    case 2:
                        for (int k = len / 2; k > 0 && s[k - 1] == '\0'; k--)
                        {
                            len -= 2;
                        }
                        break;

                    case 4:
                        var ip = (int *)s; for (int k = len / 4; k > 0 && ip[k - 1] == '\0'; k--)
                        {
                            len -= 4;
                        }
                        break;
                    }
                    return(enc.GetString(b, len));
                    //note: don't parse HTML format here. Let caller use GetHtml or parse itself.
                }
            }
        }