Esempio n. 1
0
        /// <summary>
        /// Gets content of the system clipboard.
        /// </summary>
        /// <param name="dataType">The type of the text data in the clipboard</param>
        /// <returns>Text content retrieved from the clipboard if available. Otherwise null.</returns>
        /// <remarks>
        /// <para>
        /// This method gets text from the system clipboard.
        /// If stored text data is a special format (line or rectangle,)
        /// its data type will be set to <paramref name="dataType"/> parameter.
        /// </para>
        /// </remarks>
        /// <seealso cref="Sgry.Azuki.TextDataType">TextDataType enum</seealso>
        public string GetClipboardText(out TextDataType dataType)
        {
            Int32  rc;            // result code
            bool   clipboardOpened = false;
            IntPtr dataHandle      = IntPtr.Zero;
            IntPtr dataPtr         = IntPtr.Zero;
            uint   formatID        = UInt32.MaxValue;
            string data            = null;

            dataType = TextDataType.Normal;

            try
            {
                // open clipboard
                rc = WinApi.OpenClipboard(IntPtr.Zero);
                if (rc == 0)
                {
                    return(null);
                }
                clipboardOpened = true;

                // distinguish type of data in the clipboard
                if (WinApi.IsClipboardFormatAvailable(_CF_LINEOBJECT) != 0)
                {
                    formatID = WinApi.CF_UNICODETEXT;
                    dataType = TextDataType.Line;
                }
                else if (WinApi.IsClipboardFormatAvailable(_CF_RECTSELECT) != 0)
                {
                    formatID = WinApi.CF_UNICODETEXT;
                    dataType = TextDataType.Rectangle;
                }
                else if (WinApi.IsClipboardFormatAvailable(WinApi.CF_UNICODETEXT) != 0)
                {
                    formatID = WinApi.CF_UNICODETEXT;
                }
                else if (WinApi.IsClipboardFormatAvailable(WinApi.CF_TEXT) != 0)
                {
                    formatID = WinApi.CF_TEXT;
                }
                if (formatID == UInt32.MaxValue)
                {
                    return(null);                    // no text data was in clipboard
                }

                // get handle of the clipboard data
                dataHandle = WinApi.GetClipboardData(formatID);
                if (dataHandle == IntPtr.Zero)
                {
                    return(null);
                }

                // get data pointer by locking the handle
                dataPtr = Utl.MyGlobalLock(dataHandle);
                if (dataPtr == IntPtr.Zero)
                {
                    return(null);
                }

                // retrieve data
                if (formatID == WinApi.CF_TEXT)
                {
                    data = Utl.MyPtrToStringAnsi(dataPtr);
                }
                else
                {
                    data = Marshal.PtrToStringUni(dataPtr);
                }
            }
            finally
            {
                // unlock handle
                if (dataPtr != IntPtr.Zero)
                {
                    Utl.MyGlobalUnlock(dataHandle);
                }
                if (clipboardOpened)
                {
                    WinApi.CloseClipboard();
                }
            }

            return(data);
        }