Esempio n. 1
0
        /// <summary>
        /// Sets content of the system clipboard.
        /// </summary>
        /// <param name="text">Text data to set.</param>
        /// <param name="dataType">Type of the data to set.</param>
        /// <remarks>
        ///   <para>
        ///   This method set content of the system clipboard. If <paramref name="dataType"/> is
        ///   TextDataType.Normal, the text data will be just a character sequence. If
        ///   <paramref name="dataType"/> is TextDataType.Line or TextDataType.Rectangle, stored
        ///   text data would be special format that is compatible with Microsoft Visual Studio.
        /// </para>
        /// </remarks>
        public void SetClipboardText(string text, TextDataType dataType)
        {
            Int32  rc;            // result code
            IntPtr dataHdl;
            bool   clipboardOpened = false;

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

                // clear clipboard first
                WinApi.EmptyClipboard();

                // set normal text data
                dataHdl = Marshal.StringToHGlobalUni(text);
                WinApi.SetClipboardData(WinApi.CF_UNICODETEXT, dataHdl);

                // set addional text data
                if (dataType == TextDataType.Line)
                {
                    // allocate dummy text (this is needed for PocketPC)
                    dataHdl = Marshal.StringToHGlobalUni("");
                    WinApi.SetClipboardData(_CF_LINEOBJECT, dataHdl);
                }
                else if (dataType == TextDataType.Rectangle)
                {
                    // allocate dummy text (this is needed for PocketPC)
                    dataHdl = Marshal.StringToHGlobalUni("");
                    WinApi.SetClipboardData(_CF_RECTSELECT, dataHdl);
                }
            }
            finally
            {
                if (clipboardOpened)
                {
                    WinApi.CloseClipboard();
                }
            }
        }